9
votes

I'm having trouble with some URL rewriting.

All of the stuff below works fine, but I need to add a rule which removes querystrings from URLS.

site.com/page?a=b will become site.com/page

Can someone help out? I have done some reading on .htaccess but I find it terribly complex. Also, will need to know where in the file my new directives should appear.

Thanks.


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

    # Simple 404 for missing files
    
      ErrorDocument 404 "File Not Found"
    

    # Rewriting will likely already be on, uncomment if it isnt
    
    RewriteEngine On
    RewriteBase /
    

    # Block access to "hidden" directories whose names begin with a period. This
    # includes directories used by version control systems such as Subversion or Git.
    
      RewriteRule "(^|/)\." - [F]
    

    # remove the www - Uncomment to activate
    #
    #  RewriteCond %{HTTPS} !=on
    #  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    # 

    # Remove the trailing slash to paths without an extension
    # Uncomment to activate
    # 
    #   RewriteRule ^(.*)/$ /$1 [R=301,L]
    # 

    # Remove index.php
    # Uses the "include method"
    # http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
    # 
    RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(home|inc|publishers|sidebars|about|include-template|testing|advertisers|products|sitemap|style|ad-choices|social-bar|search|404||members|P[0-9]{2,8}) [NC]
    RewriteRule (.*) /index.php/$1 [L]

2
just add a ? after $1 in your last rule. RewriteRule (.*) /index.php/$1? [L]Virendra

2 Answers

9
votes

This would remove query string from url

RewriteRule ^(.*) /index.php/$1? [L] #remove query string

Hope it helps

3
votes

I'm a little late for an answer, but since i was searching for a similar behaviour, i thought i should share it: you can also add a flag to a rewrite rule to remove the query string; the flag is [QSD], and it helps avoiding workarounds like the ? at the end ;-)

Here you can find more about this flag. I feel like pointing out that "This flag is available in [Apache] version 2.4.0 and later"