Mira Dahn
Mira Dahn

Reputation: 11

.htaccess rewrite condition for specific subpages

Currently my htaccess code is this:

RewriteCond %{HTTP_HOST} ^websiteone\.ca$ [OR]
RewriteCond %{HTTP_HOST} ^www\.websiteone\.ca$
RewriteRule ^(.*)$ "https\:\/\/websitetwo\.com\/$1" [R=301,L]

This is redirecting the homepage properly, but I want to redirect sub pages as well and currently they are not redirecting.

I don't want to redirect all subpages to corresponding pages on the new site, I want to redirect them individually to specific URLs. There are about 10 pages total.

My questions are:

  1. What are all those slashes doing?
  2. Is it possible to add multiple rewrite rules or do I have to find a way to do it all with one? My host told me to add multiple rules but I'm reading online that can cause errors.

This is what they suggested I add (not showing all pages in the list):

RewriteCond %{HTTP_HOST} ^websiteone\.ca [OR]
RewriteCond %{HTTP_HOST} ^www\.websiteone\.ca
RewriteRule ^(.*)$ "https\:\/\/websitetwo\.com\/$1" [R=301,NC]

RewriteCond %{HTTP_HOST} ^websiteone\.ca/corporate-responsibility/ [OR]
RewriteCond %{HTTP_HOST} ^www\.websiteone\.ca/corporate-responsibility/$
RewriteRule ^(.*)$ "https\:\/\/websitetwo\.com/corporate-responsibility/\/$1" [R=301, NC, L]

RewriteCond %{HTTP_HOST} ^websiteone\.ca/contact-us/ [OR]
RewriteCond %{HTTP_HOST} ^www\.websiteone\.ca/contact-us/$
RewriteRule ^(.*)$ "https\:\/\/websitetwo\.com/contact-us/\/$1" [R=301, NC, L]

Will that work?

Upvotes: 1

Views: 770

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteCond %{HTTP_HOST} ^websiteone\.ca$ [OR]
RewriteCond %{HTTP_HOST} ^www\.websiteone\.ca$
RewriteRule ^(.*)$ "https\:\/\/websitetwo\.com\/$1" [R=301,L]

This already redirects all (sub)pages, not just the homepage, to the same URL-path at the other website. Since you only want to redirect the homepage here then you should match just the empty URL-path, not any URL-path.

The two RewriteCond directives can be combined into one.

RewriteCond %{HTTP_HOST} ^websiteone\.ca/corporate-responsibility/ [OR]

Note that the HTTP_HOST server variable contains the value of the Host HTTP request header, ie. the hostname. It does not contain the URL-path. So, the above condition would never match.

You should match the URL-path in the RewriteRule pattern (1st argument).

  1. What are all those slashes doing?

They are backslash-escapes. Required (in some cases) to match the literal character in the regular expression. eg. a dot is a special character (that matches any character) so must be backslash-escaped to match a literal dot. eg. \.. However, all the backslashes in the RewriteRule substitution string are not required.

So, to redirect only the homepage, you would write it like this:

# Redirect homepage only
RewriteCond %{HTTP_HOST} ^(www\.)?websiteone\.ca [NC]
RewriteRule ^$ https://websitetwo.com/ [R=301,L]
  1. Is it possible to add multiple rewrite rules or do I have to find a way to do it all with one?

Yes, you can add multiple rules... one for each redirect. Since you are only wanting to redirect specific URLs, separate rules are probably the way to go.

Although, you do seem to be redirecting to the same URL-path for the URLs you do want to redirect and since you are having to check the requested hostname for each redirect then you could combine these into a single rule if you wish - I'll include both examples below.

...but I'm reading online that can cause errors.

Well, you may need to make sure the rules are in the correct order to avoid conflicts since the rules are processed top to bottom and the first match wins. But otherwise, this would not cause errors. (You have no option to have separate rules, just like if you were to write this redirects in your application.)

RewriteRule ^(.*)$ "https\:\/\/websitetwo\.com/corporate-responsibility/\/$1" [R=301, NC, L]

I'm not sure why you are capturing the URL-path here and appending this ($1) to the end of the target URL (complete with a double slash)? This would result in the URL https://websitetwo.com/corporate-responsibility//corporate-responsibility/ - which I'm sure is not the intention. The spaces between the flags ([R=301, NC, L]) is also invalid and will result in an error.

So, bringing the above points together...

Solution #1

With multiple rules, you can do it as follows:

# Redirect homepage only
RewriteCond %{HTTP_HOST} ^(www\.)?websiteone\.ca [NC]
RewriteRule ^$ https://websitetwo.com/ [R=301,L]

# Redirect "/corporate-responsibility/"
RewriteCond %{HTTP_HOST} ^(www\.)?websiteone\.ca [NC]
RewriteRule ^(corporate-responsibility/)$ https://websitetwo.com/$1 [NC,R=301,L]

# Redirect "/contact-us/"
RewriteCond %{HTTP_HOST} ^(www\.)?websiteone\.ca [NC]
RewriteRule ^(contact-us/)$ https://websitetwo.com/$1 [NC,R=301,L]

The $1 backreference contains the string captured from the first capturing group in the RewriteRule pattern. ie. "corporate-responsibility/" or "contact-us/" in the examples above.

Test first with 302 (temporary) redirects to avoid potential caching issues. Only change to 301 (permanent) once you have confirmed it works as intended.

Solution #2

Since you are redirecting to the same URL-path at the target site then you can combine these into a single rule using regex alternation. For example:

# Redirect homepage, "corporate-responsibility/" or "contact-us/"
RewriteCond %{HTTP_HOST} ^(www\.)?websiteone\.ca [NC]
RewriteRule ^(corporate-responsibility/|contact-us/)?$ https://websitetwo.com/$1 [NC,R=301,L]

Although that could become unreadable and error prone if you have many (even just 10) URLs to redirect.

Solution #3

You could alternatively do it like this, again with a single rule:

# Redirect homepage, "corporate-responsibility/" or "contact-us/"
RewriteCond %{HTTP_HOST} ^(www\.)?websiteone\.ca [NC]
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/corporate-responsibility/$ [OR]
RewriteCond %{REQUEST_URI} ^/contact-us/$
RewriteRule ^ https://websitetwo.com%{REQUEST_URI} [NC,R=301,L]

Add a new condition (RewriteCond directive) for each URL-path you want to redirect and it will redirect to the same URL-path at the new site.

Note there is no OR flag on the last condition.

Upvotes: 1

Related Questions