我在我的 .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]