dongwei3336 2012-02-07 16:44
浏览 22
已采纳

mod_rewrite规则删除comment.php

I need to have

mysite.com/comment.php?id=sasdfkjsfj

redirected to

mysite.com/sasdfkjsfj.

Right now, i'm using this code for doing that

RewriteEngine on
RewriteBase / 

#redirect to remove comment.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /comment\.php\?id=([^\ &]+) [NC]  
RewriteRule ^ %1? [L,R=301] 

#process the SEF Url with comment.php
RewriteRule ^([-a-zA-Z]+)$ comment.php?id=$1 [L]

It changes the url address to

mysite.com/sasdfkjsfj.

but that results a 404 page error. I have a separate comment.php page which is doing the job for

 mysite.com/comment.php?id=sasdfkjsfj

where i get the id by $_GET['id'] from the url but now, how to handle

mysite.com/sasdfkjsfj

after redirection.

And also what changes i have to make for this type of URL

mysite.com/sasdfkjsfj/Url_redirect_option.
  • 写回答

1条回答 默认 最新

  • dtbc37573 2012-02-07 17:06
    关注

    I'm just going to presume you want to redirect mysite.com/sasdfkjsfj to mysite.com/comment.php?id=sasdfkjsfj; the reverese would be unusual.

    Add the following .htaccess into the base web directory, or into your apache config:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # if you want to use /comment/ as a base dir, as suggested by ThinkingMonkey
    # then you could use this instead
    # RewriteRule ^comment/([a-zA-Z0-9_]+)$ /comment.php?id=$1 [L]
    
    # look for "mysite.com/anyasciichars" without a . or a /
    # the [L] means this is the last rule processed (if it matches)
    RewriteRule ^([a-zA-Z0-9_]+)$ /comment.php?id=$1 [L]
    </IfModule>
    

    To handle the case of mysite.com/sasdfkjsfj/rewrite_rule, do something like this:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # look for "mysite.com/anyasciichars/rewrite_rule"
    # the [L] means this is the last rule processed (if it matches)
    RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /comment.php?id=$1&rule=$2 [L]
    </IfModule>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题