简体   繁体   中英

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:

http://www.example.com/index.php?login=true , it goes to 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 ).

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. 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:

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

The following should work although there might something wrong with the line to exclude /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

This tester is cool but does have a few bugs in it: http://martinmelin.se/rewrite-rule-tester/

You need to check for the QUERY_STRING in a RewriteCond. 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

try the following in the .htaccess in root directory of example.com

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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