I thoroughly searched stackoverflow and found about 100 mod_rewrite-questions, but this was not covered...strange as it seems pretty common.
My problem: I have a web-application on a master-domain (mywebapp.com). Now I have additional country-specific TLDs (eg .ch) which I'd like to redirect on the .com-domain. Nothing special so far. But now to the other criteria:
Requirements
Examples
So to put it all together some examples how it should be:
(spaces in urls are intentional to prevent stackoverflow from generating links)
and so on. I really only need to replace the TLD in protocol-generic manner. Of course it would be nice if the rule-set is SEO-conform (301).
Additional Information / details
The domains are currently set to point to the same webserver/IP which I have root-access upon. The server is managed by ISPConfig, but II can edit the vhosts manually as well.
Thanks already in advance!
cheers, Pascal
I think the following would do the trick:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !.*\.com
RewriteCond %{HTTPS} ^on
RewriteCond %{HTTP_HOST} (?:([^.]+)\.)?mywebapp\..*
RewriteRule ^.* https://%1mywebapp.com%{REQUEST_URI} [R=302,L] #removed double slash
RewriteCond %{HTTP_HOST} !.*\.com
RewriteCond %{HTTPS} !^on
RewriteCond %{HTTP_HOST} (?:([^.]+)\.)?mywebapp\..*
RewriteRule ^.* http://%1mywebapp.com%{REQUEST_URI} [R=302,L] #removed double slash
Here's what it's saying:
RewriteRule
only if %{HTTP_HOST}
does not contain .com$1
).com
, preceed it with subdomain, concatenate the %{REQUEST_URI}
and then redirect (I left it as a 302 Temporary Redirect
so you can play with it as needed. I would go to a 301
until you are 100% ready)Edit: I duplicated the rule set to handle the protocol issue. So apparently you can only get the last backreference. Meaning that when the protocol was https, it would overwrite the subdomain stored in %1
backreference. By having a ruleset for either protocol, you circumvent the issue. Likely not the prettiest way, but it should get the job done.