Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

301 redirect all except robots.txt file


In my htaccess I have redirect all pages from my domain to main page.

I would like just to exclude from this redirect robots.txt file and url like: http://www.example.com/?123 to http://www.example.com/. Is it possible?

I have tried following .htaccess rules:

RewriteRule .+ http://www.example.com [r=301,nc,l]

Solution

  • With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/. [OR]
    RewriteCond %{QUERY_STRING} .
    RewriteCond %{THE_REQUEST} !\s(?:\S*)/robots\.txt/?\s [NC]
    RewriteRule ^ http://www.example.com [QSD,R=301,L]