Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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/.*
share|improve this answer
    
Thanks, makes sense! It kinda works now, but I have this problem: RewriteCond %{HTTP_HOST} www.secondary.ext$ [NC] RewriteRule ^(.*)$ secondary$1 [L] if i visit secondary.ext it works, if i visit www.secondary.ext it does not(internal server error)! if i remove www. from the rewritecond both stop working.. –  toefel Jan 28 '11 at 12:00
    
Since you're doing this in .htaccess you'll need to add RewriteBase / so that apache knows how it got to the secondary folder. I'm not sure why it would only have problems with www.secondary.ext though, it should either loop or not loop. Do you have access to the log to see what the internal error is? –  DerfK Jan 28 '11 at 17:30
    
I do not have access to the apache logs, using RewriteBase does not work either. I have to recreate it on a local server and read the error messages, if any. I just redirect www.toefel.nl to toefel.nl and that works for now. –  toefel Jan 28 '11 at 22:48

Changing the host will always cause Apache to create an external redirect.

Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.