Search code examples
.htaccesshttp-redirecturl-rewritingquery-stringquery-variables

How to redirect Urls with variables to the main domain using htaccess


Using .htaccess, I have tried some things to redirect Urls with variables to main the domain www.my_domain.tld, but I didn't find the right way.

I have 2 kinds of Urls that I want to redirect to my main domain:

  • http://www.my_domain.tld/blog/?p=125 ("125" could be any number from 1 to 999)
  • http://www.my_domain.tld/blog/feed/?p=72 ("72" could be any number from 1 to 999)

What I am doing wrong?


Solution

  • You can use the following rule in /.htaccess :

    RewriteEngine on
    
    
    RewriteCond %{THE_REQUEST} /blog/\?p=([0-9]+) [OR]
    RewriteCond %{THE_REQUEST} /blog/feed/\?p=([0-9]+) [NC]
    RewriteRule ^ http://domain.tld? [L,R,NC]
    

    Empty question mark at the end of the destination url is important as it discards the old query strings.