Question

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

  • the protocol should be left unchanged
  • the query-string should be left unchanged
  • the subdomain-part should be left unchanged as the tenants are identified by those
  • only exchange the TLD!

Examples

So to put it all together some examples how it should be:

  • http:// demo.mywebapp.ch -> http:// demo.mywebapp.com
  • http:// other.mywebapp.ch/user/profile -> http:// other.mywebapp.com/user/profile
  • https:// third.mywebapp.ch/about -> https:// third.mywebapp.com/about

(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

Was it helpful?

Solution 2

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:

  1. Create a RewriteRule only if %{HTTP_HOST} does not contain .com
  2. check protocol and only continue if is https
  3. Run a regex to check if subdomain exists and store subdomain if it does (referenced by $1)
  4. Manually force the .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.

OTHER TIPS

I had problems with this script.
For example it would work fine normally, like:
domain.net => domain.com
domain.net/something => domain.com/something

but when I would try with a subdomain AND a different top level domain then this would happen
sub.domain.org => subdomain.com
but
sub.domain.com => sub.domain.com

So I changed the code, here is my version working:

RewriteCond %{HTTP_HOST} !.*\.com
RewriteCond %{HTTPS} ^on
RewriteCond %{HTTP_HOST} (?:([^.]+\.))?mywebapp\..*
RewriteRule ^.* https://%1mywebapp.com%{REQUEST_URI} [R=302,L]

RewriteCond %{HTTP_HOST} !.*\.com
RewriteCond %{HTTPS} !^on
RewriteCond %{HTTP_HOST} (?:([^.]+\.))?mywebapp\..*
RewriteRule ^.* http://%1mywebapp.com%{REQUEST_URI} [R=302,L]

Hope this helps someone, rt-2

P.S. I would have added my comment to the current answer, but it said I didn't have enough reputation, for some reason.

My issue was little different but I was the impression that RewriteRule is causing protocol switch from https to http , even when i had canonicalname as on. The property which did the trick for me is RequestHeader set ClientProtocol https , when LTM was doing the SSL off load.

Listen 8085
<VirtualHost *:8085>
UseCanonicalName On
ServerName https://vip-name
ProxyPreserveHost On
RequestHeader set ClientProtocol https 
RewriteEngine On
#RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/abc$ 
RewriteCond %{REQUEST_URI} ^/xyz$
RewriteRule ^ - [skip=1]
RewriteRule ^/xyz$  https://vip-name/xyz/bc/gui/xyz/language=EN$1 [R,L]
RewriteRule ^/xyz(.*) http://ip1:8085$0 [P,NC]
RewriteRule ^/(.*) http://ip2:8086$0 [P,NC]
</VirtualHost>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top