mod_rewrite规则删除comment.php

时间:2012-02-07 16:44:56

标签: php apache .htaccess mod-rewrite url-rewriting

我需要

mysite.com/comment.php?id=sasdfkjsfj

重定向到

mysite.com/sasdfkjsfj.

现在,我正在使用此代码执行此操作

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]

它将网址改为

mysite.com/sasdfkjsfj.

但是会导致404页面错误。我有一个单独的comment.php页面正在为

工作
 mysite.com/comment.php?id=sasdfkjsfj

我从网址获取$ _GET ['id']的ID,但现在,如何处理

mysite.com/sasdfkjsfj
重定向后

此外,我必须对此类网址进行哪些更改

mysite.com/sasdfkjsfj/Url_redirect_option.

1 个答案:

答案 0 :(得分:1)

我只是假设您要将mysite.com/sasdfkjsfj重定向到mysite.com/comment.php?id=sasdfkjsfj;崇敬是不寻常的。

将以下.htaccess添加到基本web目录或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>

要处理mysite.com/sasdfkjsfj/rewrite_rule的情况,请执行以下操作:

<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>