1

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

PLM57
  • 1,256
  • 12
  • 27

3 Answers3

2

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.

0

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.

Sam Miller
  • 166
  • 5
  • Thanks a lot Sam! Two problems are left: 1. Your last line doesn't need a slash before the request_uri...if left like that, I had two slashes after the redirect. Removing the last slash in the rewriterule solved the problem. 2. subdomains don't work yet. If i call test.mywebapp.ch I get redirected to my mywebapp.com (without any subdomain). Do you have an idea how to solve this? Query-parts on the other hand work flawlessly and I learned a lot looking at your solution. Thanks for taking your time! – PLM57 Apr 09 '14 at 11:48
  • Third problem, sorry: The protocol is now hardcoded in the rewriterule. I need this to be flexibel as both ssl and non-ssl are used. Can I save this in a var and the add it to the rewrite rule as well? – PLM57 Apr 09 '14 at 11:58
  • @PLM57: I updated the answer to account for `http` or `https`. I noticed I was using `$` instead of `%` for backreferencing as well which was likely causing the subdomain to not be included. Let me know how it works out for you. – Sam Miller Apr 09 '14 at 16:15
  • Thanks sam! I couldn't test the protocol yet. the subdomain-party doesn't seem to work though. I get forwarded to the root-domain only (bla.mywebapp.ch -> mywebapp.com). This rule i found works for 1 level of subdomain only: RewriteCond %{HTTP_HOST} ^([^\.]+)\.mywebapp\..* Anyway...as soon as i add the protocol rule, also the subdomains don't work anymore either. I don't know why!? I think this is on a good path though. Any ideas? – PLM57 Apr 11 '14 at 11:38
  • Further success: after experimenting, i found the rule solving the subdomain-problem. It allows alpha-only-subdomains (unlimited number, or no subdomain at all). i updated your answer. Only problem lefdt: rule not cooperating with the protocol rule. As soon as i include it, nothing works anymore... – PLM57 Apr 11 '14 at 12:18
  • @PLM57 glad we're making progress! I've updated the answer to handle the protocol. I did a little research and it turns out you can't get backreferences from multiple RewriteCond's. Anyway, the updated code should do it for you-let me know how it goes. – Sam Miller Apr 11 '14 at 15:33
  • Performance is not an issue. Its just to catch the guys initially calling the webapp with the local-domain. Further work and bookmarks will be on the com-domain. I rechanged the subdomain-matching as your solution didn't do the trick. the rule now defined checks the validity of subdomains (except length, which is very slow in regex). As the chooseable tenant-name (which is the subdomain) is validated, this is no problem though. I also moved to 301 for permanent redirect. If you accept the edit, i can mark your answer as the solution. Thank you so much for your help! – PLM57 Apr 14 '14 at 09:53
  • Glad you got it working. Still not sure why my subdomain regex wasn't working for you though, sorry. It looks like I can't accept your edits though, not sure why. Anyway, glad it's working for you and good luck. – Sam Miller Apr 14 '14 at 14:54
0

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>
Rakesh
  • 1
  • 2