Last Updated on July 28, 2021 by Amit

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 , (for example : You have a domain name xyz.com and you want all traffic to this domain to route to a domain on remote host) then you can use RedirectMatch .

Ok , here in the first example 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 https://example.com

The code above redirects your site.com/foobar (excluding /page2) to your example.com/ .

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

RedirectMatch 301 ^/(?!page.php).*)$ https://example.com/$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 then unlike RedirectMatch RewriteRule won’t get overridden 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 https://example.com [L,R=301]

The rule above redirects all requests except /page1 from your domain to the homepage of example.com 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).*)$ https://example.com/$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 .* https://example.com/$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 .* https://example.com/$1 [L,R=301]

Related : Mod-rewrite and htaccess tutorial for beginners

I hope this article was helpful! Thanks for reading!

How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 34

No votes so far! Be the first to rate this post.