Forum Moderators: phranque
I have spent about 2 days searching this topic, and have not found an answer that speaks to my exact need.
Here's the situation: I have a primary domain, www.example.com, that resolves to my server's root (/). I also have a number of other domains (www.2nd-example.com, www.3rd-example.com, etc.), all of which must resolve to the same subdirectory within the root (/others/).
I have a rewrite in place which accomplishes most of this, but the browser's address bar shows the actual server path, i.e. www.example.com/others/
I want the address bar to show the requested domain without the directory instead, and not the primary domain/directory. Essentially simulating the 'Virtual Hosts' feature of httpd.conf. (we don't have access to that file on our hosting, or else I'd do it there.)
How is this done? many thanks in advance!
It sounds like the code (or other mechanism) used to do this is faulty, as it should internally rewrite requests for those domains to the subdirectory, rather than externally redirecting them.
An external redirect involves sending an HTTP response to the client (browser or robot) asking it to start a new HTTP transaction and ask again for the previously-requested resource using a new URL, and so changes the browser's address bar; An internal rewrite operates entirely within the context of the current HTTP transaction, and does not inform the client that the content is being served from a 'non-default' location in the server's filesystem.
An external redirect is a URL-to-URL translation, while an internal rewrite is a URL-to-filepath translation.
Jim
RewriteCond %{HTTP_HOST} ^www.example2.com
RewriteRule ^(.*)$ http://www.example.com/others/ [R=permanent,L]
I totally get what you're saying, I just haven't found an example of how to to this redirect and rewrite correctly. Essentially the code above works, but it leaves the original domain in the browser. I'd like it to display the content in the /others/ directory, while showing only www.example2.com/whatever-file.htm in the address bar.
Many thanks!
RewriteCond $1 !^others/
RewriteCond %{HTTP_HOST} ^www\.example2\.com
RewriteRule (.*) /others/$1 [L]
If you want the add-on domains to have their own subdirectories, you could use either:
RewriteCond $1 !^others/
RewriteCond %{HTTP_HOST} ^www\.(example2\.com)
RewriteRule (.*) /others/%1/$1 [L]
RewriteCond $1 !^addon_
RewriteCond %{HTTP_HOST} ^www\.(example2\.com) [OR]
RewriteCond %{HTTP_HOST} ^www\.(example3\.com) [OR]
RewriteCond %{HTTP_HOST} ^www\.(example4\.com)
RewriteRule (.*) /addon_%1/$1 [L]
I structured the code assuming that you will add additional [OR]ed RewriteConds to each rule to test HTTP_HOST for the other domains, as in the final example. Therefore, the parenthesized domain name is back-referenced in the RewriteRule using the %1 variable.
Be sure to completely flush your browser cache before testing.
Jim
# define an error page
ErrorDocument 404 /404.htm
# enable Symbolic links
# Options +FollowSymlinks
# turning rewrite engine on
RewriteEngine on
RewriteCond $1 !^others
RewriteCond %{HTTP_HOST} ^(example1\.net) [OR]
RewriteCond %{HTTP_HOST} ^www\.(example1\.net) [OR]
RewriteRule (.*) /others%1/$1 [L]
Thanks so much for your patient and considerate responses!
Trav
Jim