Advice on Using htaccess to Redirect Query Strings
Posted: 06 November 2009 10:04 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  72
Joined  06-21-2007

I’m using the LG .htaccess Generator to remove “index.php” from the site’s URL. Can anyone help with a recommended way to redirect ALL URLs that contain query strings to my site’s home page using my .htaccess file? Or if there’s a better way to do it than using .htaccess I’m all ears!

For example, if someone types in “http://mysite.com/?anything_here” I’d like them to be redirected to “http://mysite.com/”

Here is my current .htaccess code:

# -- LG .htaccess Generator Start --

# .htaccess generated by LG .htaccess Generator v1.0.0
# http://leevigraham.com/cms-customisation/expressionengine/addon/lg-htaccess-generator/

# secure .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Dont list files in index pages
IndexIgnore *

# EE 404 page for missing pages
ErrorDocument 404 /index.php?/404/index

# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp)$">
  
ErrorDocument 404 "File Not Found"
</FilesMatch>

RewriteEngine On

RewriteBase
/

# remove the www
RewriteCond %{HTTP_HOST} ^(www\.$) [NC]
RewriteRule
^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

# Remove index.php
# Uses the "include method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_URI} ^/(ee_default|search|embeds|site|how-it-works|features|pricing-and-signup|about-us|contact-us|news|terms-conditions|privacy-policy|sitemap|merchant-accounts|landing|master_redirect|news_test|test|404|compareus|landing|members|P[0-9]{2,8}) [NC]
RewriteRule
^(.*)$ /index.php?/$1 [L]

# Remove IE image toolbar
<FilesMatch "\.(html|htm|php)$">
  
Header set imagetoolbar "no"
</FilesMatch>

# -- LG .htaccess Generator End --

Thanks for any tips on the best way to handle this!

 Signature 

Andy Johnson
Shaping the Page | Twitter | LinkedIn

Profile
 
 
Posted: 06 November 2009 11:07 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  72
Joined  06-21-2007

I got my answer on this one after finding Leevi Graham’s blog post here:

http://newism.com.au/blog/post/56/expressionengine-campaign-monitor-google-analytics-and-url-rewrites/

I was having the same challenge that he was having with query strings being intercepted by my 404 template. I added the following code to my LG .htaccess Generator extension:

# rewrite homepage urls that have the query string
RewriteCond %{QUERY_STRING} ^utm_source
RewriteCond
%{REQUEST_URI} ^/$ [NC]
RewriteRule
(.*) /index.php?/site/index/&%{QUERY_STRING} [L]

This doesn’t rewrite ALL query strings, but the ones that are most important to me at the moment are handled.

Thankful Leevi took the time to post that information!

 Signature 

Andy Johnson
Shaping the Page | Twitter | LinkedIn

Profile
 
 
Posted: 15 January 2010 04:02 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  7
Joined  11-09-2009

Great, post, Andy.  Thank you! I implemented this a few moments ago and works like a charm!

Profile
 
 
   
 
 
‹‹ entry url title      Listing entries by month ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120606 Total Logged-in Users: 89
Total Topics: 126643 Total Anonymous Users: 35
Total Replies: 665718 Total Guests: 451
Total Posts: 792361    
Members ( View Memberlist )