Jan 21, 2010
My Digital Life Editorial Team

Remove and Strip Query String (Session ID or Question Mark) From URL with mod_rewrite (.htaccess)

A clean and SEO-friendly URL or web hyperlink address is important for easy visitors navigation and search engine optimization. However, due to many reasons and circumstances, the URL of the website or web page can be attached or appended with suffix which starts with question mark.

For example, forum, CMS (content management system) or web app may add and append session id or referral tracking to the URL, which normally starts with question mark (?) to visitors who are not accepting cookies, and that includes search search engine spiders or crawlers, which potentially causing duplicate content issue. Beside, some web tracking or traffic analytics services such as Google Analytics and FeedBurner may append tracking code to the end of URL, causing redundant URL structures.

Note: The part of URL which starts with ? is declared as query string because according to RFC 2396, in URI (Uniform Resource Identifier), a question mark (?) is used to represent the start of query string. In Apache mod_rewrite, the front part of the URL will return as %{REQUEST_URI} variable, while anything after the question mark will constitute as %{QUERY_STRING}.

mod_rewrite of Apache HTTPD server can be used to remove, strip, clear or truncate the query string from the URL. The rule of the mod_rewrite in order strip and truncate the part of URL after the question mark (together with the ? itself) can take many formats.

Here are a few example rewrite conditions and/or rewrite rules that can be used in .htaccess of the website to instruct mod_rewrite to strip and truncate the query string from URL. Try out to ensure that it works on your website before making the redirect rules live.

RewriteRule ^(.*)$ http://www.domainname.com/$1? [R=301,L]

Replace domain name with the actual domain name of the website. Or,

RewriteCond %{QUERY_STRING} ^session_id
RewriteRule ^(.*)$ $1 [L,R=301]

Replace session_id with the variable name the succeed the question mark in the URL, which is normally consistent to indicate what’s the value is for. Or,

RewriteCond %{THE_REQUEST} ^\?session_id=(.*)$ HTTP [NC]
RewriteRule ^$ http://www.domainname.com%{REQUEST_URI} [R=301,L]

Replace the domain and session_id accordingly.

Note that the above .htaccess mod_rewrite trick may not work on WordPress powered blogs which has automatic canonical links redirect enabled (which is default setting), or else it will generate infinite redirect loop.

Related posts:

  1. How to Find or Replace Question Mark (?), Asterisk (*) or Tilde (~) in Microsoft Office Excel
  2. Change or Set MySQL Long Query Time Value for log-slow-queries
  3. Hack to Modify Firefox 3 Address Bar to Return Search Results When Query or Invalid URL Is Entered
  4. Remove and Disable Google Analytics Tags (?utm_source &utm_medium) in FeedBurner URL
  5. Add Trailing Slash to the End of the URL with .htaccess Rewrite Rules

2 Comments

  • I have flash menu in my website. Whenever i click on a menu link its adding an extra query string at the end of URL like "&ref=http%3A//www.mysite-name.com/". I tried your example but didnt work for me. I want to get rid of that extra part (i.e after "ref"). Please suggest how to fix it

    RewriteCond %{QUERY_STRING} ^ref
    RewriteRule ^(.*)$ $1 [L,R=301]

  • That's what i get and I am facing the exact problem.

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, webmaster@studentask.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Leave a comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Subscribe

Free email subscriptions
Get latest updates in email for free:

Translate This Page