How can I create a rewrite rule which rewrites the browser url such that requests to
www.domain.net/sub
is always shown assub.domain.net
?No redirection of the page is needed.
Answer
If I understand you correctly, what you want is this:
- The client types in www.domain.net/sub in his browser
- The text in the address field of the browser changes to sub.domain.net
- The contents shown are still the same
If so, you must have a redirect. That’s the way to do the change in the address bar. You can’t just edit the browser address bar on somebody else’s computer.
So the first thing you do is the redirect as posted by LogicWreck.
Then you add a VirtualHost entry either to your httpd.conf or your .htaccess file, containing something like
<VirtualHost *:80>
DocumentRoot /path/to/webfiles/sub
ServerName sub.domain.net
</VirtualHost>
and probably some more directives; have a look at httpd.apache.org
You could also add another rewrite rule to find the files under the correct directory, but generally speaking a normal VirtualHOst would be better.
If you want to use a rewrite rule instead of VirtualHost, you would do something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(sub.domain.net)$
RewriteRule /(.*) /path/to/webfiles/sub/$1
But I would recommend the VirtualHost route; that’s the normal way of doing something like this.
Attribution
Source : Link , Question Author : Karl Pilz , Answer Author : Jenny D