Forum Moderators: phranque

Message Too Old, No Replies

Remove Query String from URLs Only From Home Page

         

ambition

12:56 pm on Apr 25, 2012 (gmt 0)

10+ Year Member



Hello,

I am trying to remove the query string out of urls to my home page only. I found this code from jdMorgan in another thread:

RewriteCond %{QUERY_STRING} ^([^&]*&)*page=([^&]+)
RewriteRule ^index.php$ http://www.example.com/%1? [R=301,L]

This works so I took index.php out and put in (.*)$ which made it work for all urls on the homepage, but made inner urls with the query string not work. So here is what I currently have, how can make it only affect www.sitename.com and not www.sitename.com/stuff/ hopefully without putting an .htaccess in every folder because I have a ton.

Here is what I currently have now just for reference:

RewriteCond %{QUERY_STRING} !=""
RewriteRule (.*)$ http://www.example.com/%1? [R=301,L]

Any ideas?

[edited by: incrediBILL at 7:11 pm (utc) on Apr 25, 2012]
[edit reason] fixed link [/edit]

g1smd

2:03 pm on Apr 25, 2012 (gmt 0)

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



Use example.com in this forum.

# Redirect "root" and "root named index file" requests with query string for www and non-www
# redirecting to www without index file name and without query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(index\.(php|html?))?$ http://www.example.com/? [R=301,L]


This redirects to www URL ending with / and without query string when query string is requested for "root" and for "named index file in root" on www or non-www.

Follow it with these two rules for index canonicalisation and for non-www canonicalisation:

# Redirect named index requests (without query string) on www or non-www
# redirecting to / or /folder/ on www
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(php|html?)\ HTTP
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://www.example.com/$1? [R=301,L]


# Redirect all other non-www requests to www retaining same page name
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http:/www.example.com/$1 [R=301,L]

lucy24

9:31 pm on Apr 25, 2012 (gmt 0)

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



how can make it only affect www.sitename.com and not www.sitename.com/stuff/

In case you missed it, the Magic Character in g1's post is the opening anchor ^. It means that nothing is allowed to come before the "index.xtn".

ambition

10:17 pm on Apr 25, 2012 (gmt 0)

10+ Year Member



This example worked perfectly! Thank you so much. It amazes me how much help is given by those here.