Search code examples
apache.htaccessmod-rewrite

Disable .htaccess redirect if localhost


I have this .htacess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://domain.co.uk/$1 [R=301,L]

This redirects any sub domain of domain.co.uk to the root domain.co.uk e.g. test.domain.co.uk goes to domain.co.uk

However I have noticed if I try to access this site locally it redirects localhost/domain/ to domain.co.uk, can I add some lines to not do the redirect if on http://localhost?


Solution

  • Yes, you can add localhost in negative condition to stop redirecting localhost like this:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^(localhost|domain\.co\.uk)$ [NC]
    RewriteRule ^ https://domain.co.uk%{REQUEST_URI} [R=301,L,NE]