繁体   English   中英

修改.htaccess以使URL?variable = true更改为URL / variable?

[英]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

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

您需要在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?= true的任何内容重定向到/ action

在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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM