我在.htaccess文件中使用以下内容,但它似乎无法正常工作。
Redirect 301 top_users.php http://example.com/top-users
RewriteRule ^top-users/?$ top_users.php
Redirect 301 register.php http://example.com/register
RewriteRule ^register/?$ register.php
Redirect 301 register.php?log=1 http://example.com/login
RewriteRule ^login/?$ register.php?log=1
顶级用户页面似乎工作正常,如果我直接进入/ login或/ register,它工作正常,但重定向不起作用。我尝试在重定向页面名称之前添加正斜杠,但它给了我错误的路径并说它无法找到。
有什么想法吗?
编辑:
更新:
我使用的最终代码如下,感谢Jon Lin的帮助。
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /top-users(/|$|\ )
RewriteRule ^top-users(/|$|\ ) top_users.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /top_users\.php
RewriteRule ^top_users\.php$ http://example.com/top-users/ [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /register(/|$|\ )
RewriteRule ^register(/|$|\ ) register.php [L]
# check if request for register.php does not include log=1 in query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /register\.php
RewriteCond %{QUERY_STRING} !log=1
RewriteRule ^register\.php$ http://example.com/register/ [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /login(/|$|\ )
RewriteRule ^login(/|$|\ ) register.php?log=1 [L,QSA]
# check if request for register.php includes log=1 in query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /register\.php
RewriteRule ^register\.php$ http://example.com/login/? [R=301]
答案 0 :(得分:1)
您正在导致重定向循环。当原始请求用于php文件时,您应该重定向,并在原始请求用于看起来更好的URI时重写:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /top-users(/|$|\ )
RewriteRule ^top-users top-users.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /top-users\.php
RewriteRule ^top-users\.php$ http://example.com/top-users/ [R=301]
etc...
对于其他2个,您需要匹配查询字符串:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /register(/|$|\ )
RewriteRule ^register register.php [L]
# check if request for register.php does not include log=1 in query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /register\.php
RewriteCond %{QUERY_STRING} !log=1
RewriteRule ^register\.php$ http://example.com/register/ [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /login(/|$|\ )
RewriteRule ^login register.php?log=1 [L,QSA]
# check if request for register.php includes log=1 in query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /register\.php
RewriteCond %{QUERY_STRING} (^|&)log=1(&|$)
RewriteRule ^register\.php$ http://example.com/login/ [R=301]
您无法与Redirect
或RewriteRule
中的查询字符串进行匹配。您必须使用RewriteCond %{QUERY_STRING}
答案 1 :(得分:0)
为什么不使用mod_rewrite标志?您可以按以下方式将每两行编辑为一行
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteRule ^top-users(/?)$ top_users.php [L,QSA,R=301]
</IfModule>
QSA
代表查询字符串附加,对于任何GET参数,使用R=301
替换每个规则的前一行。