0

重写页面,如:

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

1 回答 1

0

这条规则应该有效:

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

确保在不同的浏览器中进行测试以避免 301 缓存陷阱。

确认它工作正常后,替换R=302R=301. R=301在测试你的 mod_rewrite 规则时避免使用(永久重定向)。

建议的 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>
于 2013-10-11T09:35:44.330 回答