Rewrite HOST without redirecting to the rewritten hostname, is that possible

apache-2.2mod-rewrite

I only have one virtual host available with my service provider the folder structure is similar to this:

  • domains

    • primarydomain.ext

      • stat
      • www
      • secondary (<- this is the folder for "secondary.primarydomain.ext")

I have the domain "primarydomain.ext" registered and working. I have a second domain "secondary.ext" registered somewhere else, and pointing to the same web server. The service provider allowed this and now it acts like an alias to primarydomain.ext. I want to use mod_rewrite to internally rewrite "secondary.ext" to "secondary.primarydomain.ext" and process it like that (with the folder named secondary as the root).

The url in the browser should stay "secondary.ext"! The solution I have works, it stores the following in a .htaccess file located inside the "primarydomain.ext" folder but updates the browser URL to "secondary.primarydomain.ext":

RewriteEngine On 

RewriteCond %{HTTP_HOST} secondary.ext$ [NC]
RewriteRule ^(.*)$ http://secondary.primarydomain.ext [L]

How can I fix this so that the browser URL is not updated, or whatever other solution gives me the desired effect?

(as a note: my provider is not able (does not want) to create a new virtual host for domains that are not registered by them, so they pointed me to this solution, but do not offer support for it.)

Best Answer

The answer is to not try and change HOST, but to change the path used to find files locally.

RewriteCond %{HTTP_HOST} secondary.ext$ [NC]
RewriteRule ^(.*)$ /domains/primarydomain.ext/secondary$1 [L]

Edit If you're getting an infinite loop, add another condition to exclude requests already in the secondary folder after the HTTP_HOST condition and before the Rule:

RewriteCond %{REQUEST_URI} !^/secondary/.*
Related Topic