mod rewrite - Apache / mod_rewrite: Change TLD, but keep subdomain, protocol and query-string -
i thoroughly searched stackoverflow , found 100 mod_rewrite-questions, not covered...strange seems pretty common.
my problem: have web-application on master-domain (mywebapp.com). have additional country-specific tlds (eg .ch) i'd redirect on .com-domain. nothing special far. other criteria:
requirements
- the protocol should left unchanged
- the query-string should left unchanged
- the subdomain-part should left unchanged tenants identified those
- only exchange tld!
examples
so put examples how 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 intentional prevent stackoverflow generating links)
and on. need replace tld in protocol-generic manner. of course nice if rule-set seo-conform (301).
additional information / details
the domains set point same webserver/ip have root-access upon. server managed ispconfig, ii can edit vhosts manually well.
thanks in advance!
cheers, pascal
i think following 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 it's saying:
- create
rewriterule
only if%{http_host}
not contain .com - check protocol , continue if https
- run regex check if subdomain exists , store subdomain if (referenced
$1
) - manually force
.com
, preceed subdomain, concatenate%{request_uri}
, redirect (i left302 temporary redirect
can play needed. go301
until 100% ready)
edit: duplicated rule set handle protocol issue. apparently can last backreference. meaning when protocol https, overwrite subdomain stored in %1
backreference. having ruleset either protocol, circumvent issue. not prettiest way, should job done.
Comments
Post a Comment