Forum Moderators: phranque

Message Too Old, No Replies

Redirect from root

         

vnbit

12:34 pm on Apr 27, 2011 (gmt 0)

10+ Year Member



How to redirect from the root website like this:
http://www.example.com/?any=word
to
http://www.example.com/word.html


Here is my way but not work:
RewriteRule ^\?any=(.*)$ http://www.example.com/$1.html [R=301,L]

or
RewriteRule ^index\.php\?any=(.*)$ http://www.example.com/$1.html [R=301,L]


How can i do that?

g1smd

3:31 pm on Apr 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule cannot see query string data. <-- I have typed these exact words into this forum every day for more than a week.

You need a preceding RewriteCond looking at QUERY_STRING in order to get this to work.

Your solution will also need to add a question mark to the end of the redirected URL in order to clear the query string data otherwise you will have an infinite redirect loop.

vnbit

4:50 pm on Apr 27, 2011 (gmt 0)

10+ Year Member



Sorry and thank you, sir!

Because my mother language is not English, so I am very grateful for your suggestions about keyword for searching. And i'm a newbie, hope u can sympathize with me.

How this wrong?
RewriteCond %{QUERY_STRING} &?any=([^&]+)&?
RewriteRule ^index\.php$ /%1.html? [R=301,L]


It works for
http://www.example.com/index.php?any=something
->
http://www.example.com/something.html

But not for
http://www.example.com/?any=something
->
http://www.example.com/?any=something

g1smd

6:15 pm on Apr 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It cannot work for
http://www.example.com/?any=something
because your rule says that the request must contain
index.php
and that example URL does not do so.

Change the pattern to
^(index\.php)?$
and it will then work for index.php there or not.

The redirect target must also include the protocol and domain name. Make sure you add them both to your rule.

RewriteCond %{QUERY_STRING} (^|&)any=([^&]+)(&|$)
RewriteRule ^(index\.php)?$ http://www.example.com/%2.html? [R=301,L]