修改.htaccess使URL?更改为URL/变量


Modify .htaccess to make URL?variable=true change to URL/variable?

我想修改我的。htaccess文件所以如果我进入:

http://www.example.com/index.php?login=true,转到http://www.example.com/login

我目前有这个代码删除index.php(这使得上面看起来像http://www.example.com/?login=true)。

RewriteEngine On
#remove index.php
RewriteCond %{THE_REQUEST} ^GET.*index'.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index'.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET

我将以另一种方式设置您的重写规则:

RewriteEngine On
RewriteRule ^login$ /index.php?login=true

这样,如果用户浏览到http://yourserver.com/login,实际使用的页面是http://yourserver.com/index.php?login=true,但第一个URL显示在浏览器中。我猜这就是你想达到的目的。

如果你真的需要按照你要求的方向做,你可以试着这样做:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^login=true$
RewriteRule ^index'.php$ /login [L,R=301]

如果存在额外的查询参数,该操作将失败。

如果您想将http://yourserver.com/index.php重定向到http://yourserver.com,只需添加以下重写规则:

RewriteRule ^index'.php$ / [L,R=301]

尽管排除/system/*的行可能有问题,但下面的操作应该可以工作。

RewriteEngine On
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} ^/system/.*
RewriteRule index.php /%1? [R=301,L]

下面的页面有帮助:http://wiki.apache.org/httpd/RewriteQueryString

这个测试器很酷,但确实有一些bug: http://martinmelin.se/rewrite-rule-tester/

在example.com根目录的。htaccess中尝试以下操作

RewriteEngine On
RewriteBase /
#rewrite http://www.example.com/anything to http://www.example.com/index.php?anything=true
RewriteCond %{REQUEST_URI} ^/([-a-zA-Z0-9]+)$ [NC]
RewriteCond %1 !system [NC]
RewriteRule . index.php?%1=true [L]

#301 redirect requests for example.com/index.php?anything=true to example.com/anything
RewriteCond %{REQUEST_URI} ^/index'.php$ [NC]
RewriteCond %{QUERY_STRING} ^([^=]+)=true$ [NC]
RewriteRule . /%1? [L,R=301]

您需要检查RewriteCond中的QUERY_STRING。我觉得应该这样做:

RewriteEngine On
#remove index.php
RewriteCond %{QUERY_STRING} ([^=]*)=true [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^index'.php /%1? [R=301,L]

这应该重定向任何有index.php?