-1

Struggling to figure out the best way to handle this. We currently use the following htaccess code to yield prettier urls:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ?id=$1 [QSA]
##Force SSL 
RewriteCond %{HTTP:X-Forwarded-Proto} !https

This allows us to use something like https://domain1.com/i-1234567890 and it is displayed in the browser this way but the page is served as https://domain1.com/?id=i-1234567890

We want to continue doing this but are also looking to consolidate domains. All 3 domains are already hosted on the same server via CNAME DNS entries but would like only one to be used and others redirected.

What would be the best way to go about this? For example: http://domain2.com/i-1234567890 and http://domain3.com/i-1234567890 both route to http://domain1.com/i-1234567890 while still handling the QSA and forcing https?

I'll also add that since only these domains are served on this server it is safe to redirect ALL traffic to http://domain1.com/i-1234567890 instead of having RewriteCond for each domain. I've tried a few things but they have resulted in too many redirects.

Thanks!

0

Match %{HTTP_HOST} against what you want and redirect not matching requests to the required server.

Add this just after RewriteEngine On:

RewriteCond "%{HTTP_HOST}" !="domain1.com" [NC]
RewriteRule "^(.*)$" "https://domain1.com/$1" [R=301,L]

First line checks whether HTTP Host: header is not equal ([NC] -> case insensitive) to domain1.com exactly. If the header is different, e.g. domain2.com, the condition is met and the rule on the second line is applied.

And the rule takes the whole path (^(.*)$ regexp) and redirects it ([R=301] ie HTTP Moved Permanently) to https://domain1.com/$1 where $1 is everything from the original path as matched by the previous regexp. [L] means it is the last rule to apply, so Apache stops processing the htaccess file.

When a request comes to https://domain1.com/blabla, the condition is not met and the rule is not applied, so the rest of your htaccess is processed.

So the new htaccess should look like this:

RewriteEngine On
RewriteCond "%{HTTP_HOST}" !="domain1.com" [NC]
RewriteRule "^(.*)$" "https://domain1.com/$1" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ?id=$1 [QSA]
##Force SSL 
RewriteCond %{HTTP:X-Forwarded-Proto} !https
  • Thanks! Just to be sure since I am still learning how these rules work together would this be my complete htaccess to also handle the parameter? RewriteCond "%{HTTP_HOST}" !="domain1.com" [NC] RewriteRule "^(.*)$" "domain1.com/$1" [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ ?id=$1 [QSA] ##Force SSL RewriteCond %{HTTP:X-Forwarded-Proto} !https – Steven Carlton Jan 8 '18 at 0:03
  • I have not checked your parameter rewriting, if it works for you, my part won't break it. I'm going to add more info to the answer just to be safe :-). – Ondřej Xicht Světlík Jan 8 '18 at 0:04
  • Thanks for that, and for dealing with my horribly formatted comment! – Steven Carlton Jan 8 '18 at 0:05
  • Just to be clear, I would want both the redirect and the parameter rewriting to happen for all traffic. Do I add the original parameter rewriting code after this? – Steven Carlton Jan 8 '18 at 0:23
  • Sure, I'm adding the whole configuration to the answer. – Ondřej Xicht Světlík Jan 8 '18 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.