mod_rewrite和新url结构的问题

时间:2015-03-21 15:56:55

标签: apache .htaccess mod-rewrite redirect

我想在我的网站上建立新的网址结构:
/page.php?id=1 - > /条件方面的服务
/page.php?id=2 - > /常见问题

所以我在htaccess中做了:

RewriteRule ^terms-of-service /page.php?id=1 [L,NC,QSA]

它运行良好,当我去mysite.com/terms-of-service时,我可以看到我的页码1.但我也希望从旧地址到新地址进行301重定向。当我试着这样做时:

RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^page.php$ terms-of-service? [L,R=301,NC]
RewriteRule ^terms-of-service /page.php?id=1 [L,NC,QSA]

我得到了一个"无效的重定向"我的浏览器出错。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的规则将导致重定向循环。您需要在此处使用%{THE_REQUEST}。这样做:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /page\.php\?id=1\s [NC]
RewriteRule ^ terms-of-service? [L,R=301]

RewriteRule ^terms-of-service/?$ page.php?id=1 [L,NC,QSA]