如何编写重写规则来隐藏查询字符串?

时间:2012-11-19 14:34:15

标签: php mysql .htaccess

我在重写URL以隐藏查询字符串时遇到问题。我想改写:

http://seynol.tk/comments?post_referrel_id=16

为:

http://seynol.tk/comments only

这是我到目前为止所尝试的:

RewriteBase /
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]

RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]

RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]

1 个答案:

答案 0 :(得分:0)

您所要做的就是检查查询字符串是否已设置,并使用空查询字符串进行重定向。

RewriteCond %{QUERY_STRING} post_referrel_id
RewriteRule (.*) $1? [R=301,L]

如果您想将引荐ID放在URL中并以静默方式将其转换为查询字符串,您可以使用此代码:

RewriteRule ^/comments/([0-9]+)$ /comments?post_referrel_id=$1

这假定帖子ID是数字。该网址如下所示:http://sevnol.tk/comments/16