为什么我的重写规则最后会出现问号?

时间:2013-10-11 09:33:40

标签: .htaccess redirect

重写页面,如:

mysite.com/eng/cat

重定向到:

mysite.com/eng?sentence=cat

我做了以下重写规则:

RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1? [R=301,NC,L]

但是当我要求时

mysite.com/eng/cat

它会像这样重写

mysite.com/eng?sentence=cat?

我不知道为什么它会在最后加上一个问号,我也试过了

RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1 [R=301,NC,L]

RewriteCond %{THE_REQUEST} ^GET\ /eng/([^&\s]+) [NC]
RewriteRule ^eng/ eng\?sentence=%1\? [R=301,NC,L]

没有任何区别。有什么帮助吗?

我的htaccess文件:

# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
RewriteEngine on    


RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]

RewriteRule ^ eng?sentence =%1 [R = 302,NC,L,NE]

AddType application/x-httpd-php .htm .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php



Options -Indexes
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
</FilesMatch>

1 个答案:

答案 0 :(得分:0)

这条规则应该有效:

RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ eng?sentence=%1 [R=302,NC,L,NE]

确保在其他浏览器中进行测试,以避免301缓存陷阱。

验证一切正常后,将R=302替换为R=301。在测试mod_rewrite规则时,请避免使用R=301(永久重定向)。

建议的htaccess文件:

# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html

Options -Indexes -MultiViews

ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

RewriteEngine on

RewriteCond %{THE_REQUEST} ^GET\s/+eng/([^/?&\s]+) [NC]
RewriteRule ^ /eng?sentence=%1 [R=302,NC,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
</FilesMatch>