I'm trying to find a mod_rewrite code for forcing users that go to FQDN to be re-written to the hostname without the domain and don't want them to be stuck in a loop. I can only find re-write examples for the other way round. Anyone have a suggestion on how I can do this?
So example if I was to go to http://appname.example.com/test.php
The rewritten URL should be http://appname/test.php
Any suggestions would be great.
To "redirect" http://<appname>.example.com/<url-path>
to http://<appname>/<url-path>
(where <appname>
and <url-path>
are entirely variable and <appname>
is also a resolvable hostname on the local network) then you would do something like the following using mod_rewrite at the top of the root .htaccess
file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([a-z-]+)\.example\.com [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
This excludes the www
subdomain using a negative lookahead.
%1
in the substitution string is a backreference to the <appname>
as captured from the requested hostname.
%{REQUEST_URI}
contains the full root-relative URL-path (starting with a slash).
This also handles FQDN (that end in a dot).