May 1, 2021 0 Comments

Last Updated on May 2, 2021 by amitoverflow1

htaccess redirect domain with exception

Through this article we will learn how to write a code in htaccess to redirect a domain to another domain with exception.
We will use both directives
RewriteRule and
RedirectMatch in the example codes because both directives can be used in htaccess but you should always prefer RewriteRule as it’s faster then Redirect .

Redirect domain with exception using RedirectMatch

You can use RedirectMatch directive for URL redirection as its much similar to RewriteRule but you can’t redirect based on host header using this directive.
If you need to redirect your domain to another domain on different server , then you can use RedirectMatch .

Ok , here we will use RedirectMatch to redirect our domain and all its pages to another domain but with an exception that our /page2 shouldn’t redirect.
We can use this in our htaccess :
RedirectMatch 301 !/page2

The code above redirects your (excluding /page2) to your .

If you also need to preserve the URL path , then first you capture it in pattern using regex and use the match with destination URL.

RedirectMatch 301 ^/(?!page.php).*)$$1

Redirect domain to another domain with exception using RewriteRule

I always prefer RewriteRule directive for URL redirection and rewriting because its read quickly by Apache server.
If you have other directives in your htaccess RewriteRule won’t conflict with those directives but instead it will do what it does on its own.
RewriteRule and RedirectMatch are directives of two different Apache modules. The main difference between these two directives is the runtime behavior.
RewriteRule is always read first by server regardless of where its placed in htaccess.
With RewriteRule you can also Redirect your domain based on host headers.
This is helpful especially when you have multiple domains and you want to redirect a specific one.

Okay, here is how we can convert the above RedirectMatch into RewriteRule.

RewriteEngine on
RewriteRule !page1 [L,R=301]

The rule above redirects all requests except /page1 from your domain to the homepage of domain.

If you also need to preserve the URL path , then you can use the same pattern as we used with RedirectMatch above but trim the leading slash as its not used in RewriteRule on htaccess context :

RewriteEngine on
RewriteRule ^((?!page).*)$$1 [L,R=301]

You can also use RewriteCond directive with RewriteRule to exclude the path . The code example above will look like the following with RewriteCondition :

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/page [NC]
RewriteRule .*$1 [L,R=301]

The RewriteCondition is there to exclude the specific page , so you can just use a catch-all pattern with your rule.

To exclude multiple URL paths you can use RewriteConditions with OR flag. The following is a code example to do that :

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/page [NC,OR]
RewriteCond %{REQUEST_URI} !^/page2 [NC]
RewriteRule .*$1 [L,R=301]

Related : Mod-rewrite tutorial for beginners by Amit

I hope this article was helpful! Thanks for reading!

Leave a Reply

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