.htaccess: redirect URLs containing php queries

I recently had to solve the problem of redirecting URLs with php queries, i.e. http://www.phillab.net/?page=programme&subpage=Passdraw → http://www.hasper.info/passdraw/ by using the .htaccess file.

Since most of the URLs did not have such a 1:1 translation like the example above, I did not work with wildcards – but this is also possible! I had the following constraints

  1. The php queries have to be removed from the redirected URL (this is not the standard!)
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=passdraw
    RewriteRule (.*) http://www.hasper.info/passdraw/? [R=301,L] #The question mark in the end does the trick!
    
  2. The php query check should be case-insensitive
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=passdraw [nocase]
    RewriteRule (.*) http://www.hasper.info/passdraw/? [R=301,L]
    
  3. Some queries have to point to the same new URL
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=passdraw [nocase,or]
    RewriteCond %{QUERY_STRING} subpage=bestAppEver [nocase]
    RewriteRule (.*) http://www.hasper.info/passdraw/? [R=301,L]
    
  4. Some queries contain special characters like dots or plus signs.
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=Ghostball\x2B3[nocase] #A plus sign has to be replaced by \x2B
    RewriteRule (.*) http://www.hasper.info/ghostball-3/? [R=301]
    RewriteCond %{QUERY_STRING} subpage=N\.T\. [nocase] #A dot has to be escaped by a slash
    RewriteRule (.*) http://www.hasper.info/n-t-a-commercial/? [R=301,L] #The L marks this as last rule
    

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.