简体   繁体   English

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

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

I'm trying to change my .htaccess file so that if I go to: 我试图更改我的.htaccess文件,以便如果我转到:

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

I currently have this code which removes index.php (which makes the above looks like http://www.example.com/?login=true ). 我目前拥有删除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

I would setup your rewrite rule the other way around: 我会以另一种方式设置您的重写规则:

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

This way if a user browses to http://yourserver.com/login the actual page used is http://yourserver.com/index.php?login=true , but the first URL is shown in the browser. 这样,如果用户浏览到http://yourserver.com/login,则实际使用的页面是http://yourserver.com/index.php?login=true ,但是第一个URL在浏览器中显示。 I assume this is what you are trying to achieve. 我认为这就是您要实现的目标。

If you really need to do it in the direction you asked for, you can try something like this: 如果您确实需要按照要求的方向进行操作,可以尝试执行以下操作:

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

This will fail of there are additional query parameters. 这将失败,因为还有其他查询参数。

If you want to redirect http://yourserver.com/index.php to http://yourserver.com you can simply add the following rewrite rule: 如果您要将http://yourserver.com/index.php重定向到http://yourserver.com ,则只需添加以下重写规则:

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

The following should work although there might something wrong with the line to exclude /system/*. 尽管排除/ system / *的行可能存在问题,但以下内容应该可以工作。 Try testing with that commented out. 尝试测试注释掉的内容。

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

The following page helped: http://wiki.apache.org/httpd/RewriteQueryString 以下页面提供了帮助: http : //wiki.apache.org/httpd/RewriteQueryString

This tester is cool but does have a few bugs in it: http://martinmelin.se/rewrite-rule-tester/ 这个测试器很酷,但是确实有一些错误: http : //martinmelin.se/rewrite-rule-tester/

You need to check for the QUERY_STRING in a RewriteCond. 您需要在RewriteCond中检查QUERY_STRING。 I think this should do: 我认为应该这样做:

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

This should redirect anything which has index.php?=true to /action 这应该将具有index.php?= true的任何内容重定向到/ action

try the following in the .htaccess in root directory of example.com 在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