1

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

2 回答 2

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]

您无法匹配 aRedirect或中的查询字符串RewriteRule。您必须使用RewriteCond %{QUERY_STRING}

于 2012-06-19T18:02:58.843 回答
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您替换每个规则的前一行。

于 2012-06-19T17:27:38.537 回答