Reputation: 225
what is the definitive, bulletproof, rocksolid, best practices, method in an htaccess file to map/route/redirect both "domain2.com" & "www.domain2.com" to a specific wordpress page at "domain1.com/page"?
i've searched & read a lot but can't seem to find the gold standard. there seem to be so many variations, and i haven't been able to figure out what all the bits & pieces mean enough to know which i should include & which would be best left out.
for example...
RewriteCond %{HTTP_HOST} ^domain2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ https://domain1.com/$1 [NC,R=301,L]
i understand that the first two lines takes care of the with [OR]
without "www" part.
i understand the [NC]
means "no-case", which seems important to have.
i've figured out that by having the $1
at the end, would mean if someone went to "domain2.com/page" it would send them to the equivalent "domain1.com/page" - this i should do without, since in my case i just have some extra domains that i want sent to specific pages. these extra domains have not existed as websites before.
i've learned that the [L]
and [R=301]
at the very end are necessary as the "301" is the type of redirect, and the "L" is the thing that tells it to execute (the "L"et's do it command).
the part that's really stumping me is the ^(.*)$
i haven't been able to find any descriptions breaking down what each character does. i would have just used it as-is except, it seems every example i found showed many variations of it, like...
RewriteRule ^(.*)$ https://domain1.com/page [NC,R=301,L]
RewriteRule ^(.*) https://domain1.com/page [NC,R=301,L]
RewriteRule ^ https://domain1.com/page [NC,R=301,L]
RewriteRule .* https://domain1.com/page [NC,R=301,L]
...etc.
and then just to really throw me off, one example said to have a question mark at the end to keep it from looping...
RewriteRule ^ https://www.newdomain.com/page/? [R=301,L]
i also got confused with some examples encasing this code in...
<IfModule mod_rewrite.c>...</IfModule>
...and others did not. which is correct?
lastly, i saw one post that showed you could combine the two lines of...
RewriteCond %{HTTP_HOST} ^domain2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
...into...
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain2\.com$ [NC]
...is that correct?
and on a final sidenote - Wordpress pages don't have file extensions in the url (which i like) like...
domain1.com/page
...instead of...
domain1.com/page.html
...does this throw a wrench in the "RewriteRule" url?
thanks for any/all assistance!
PS: Oh, i forgot also, what would be the most efficient way to force any/all of these domains from http to https?
Upvotes: 2
Views: 45
Reputation: 786091
For your requirement, following minimal directive rule should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ https://domain1.com/page? [R=301,L]
# http -> https redirect
RewriteCond %{HTTPS} !on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
We are using pattern as ^(www\.)?domain2\.com$
because it will match both ^www.domain2.com
and domain2.com
domains since we are making www.
part optional at the start.
We match just ^
in RewriteRule
just to make sure we match every URL in domain2
domain. We could use .*
also but ^
(matches start posotion in regex) is 1 full character shorter.
We append a ?
in the target URL to make sure any query string in old URL is stripped off. So e.g. http://domain2.com/?id=123
will be redirected to https://domain1.com/page
.
Since we are always redirecting to a fix page https://domain1.com/page
there is no need to capture original URL and reuse it later, hence no need to use $1
etc.
Upvotes: 4