views:

623

answers:

4

Is it possible to redirect all requests to x.domain.com/.* to y.domain.com/.* WITHOUT letting this redirection be visible in the url?

I have unsuccessfully tried several things in .htaccess. Just specifying the [L] flag still shows this redirection in the url (as it does when I use the [R] flag additionally).

EDIT: as somebody claimed there being no reason for this, let me give some more information :) I have one nice url: x.domain.com , which is well known. Then there are a number of other domains: spring.domain.com , summer.domain.com , autumn.domain.com, winter.domain.com . Depending on the time of the year, a specific y.domain.com becomes the current one. The x.domain.com should always map to the current one.

EDIT2: I'll write here, as the code isn't nicely rendered in the comments... I tried what Arjan suggested:

RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1

Unfortunatly though this keeps redirecting forever. :(

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

Putting the [R] flag behind, I see in the url something like:

http://x.domain.com/path/to/y.domain.folder/path/to/y.domain.folder/path/to/y.domain.folder/ ...

Any suggestions?

Now that I can read the errorlogs, I can give a direct response, as what a possible 500 error refers to.

+1  A: 

Have you tried Alias /dir/file.html /full/path/to/other/file.html ??

jetru
yes, this gives me a 500 error :( ... but thanks anyway.
jacob
the specific error is: Alias not allowed here
jacob
A: 

To my knowledge and testing with firebug a redirect via .htaccess is always announced to the client and it's up to him how to proceed. It is therefore not an alternative to some sort of SSI functionality. To prevent a "fake" address modern browser should always make the REAL address visible to the user, however I think I have seen some misbehavior in programs like "feeddemon" where IE is embedded. If you - for whatever reason - really want to show content from one subdomain on another you can try using Javascript or (i)frames on the user side or some include functionality on the server site (eg. file_get_contents with php). However, I don't recommend this.

merkuro
+1  A: 

If these are not hosted on the same server, then you'd need the Proxy flag. This also requires the proxy module to be running. Not tested:

RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ http://y.domain.com/$1 [P]

EDIT: Given the edits to your question they're probably just on the same server. So then indeed, as jetru suggested an Alias might do. Or:

# No RewriteCond required; serve all content from other folder:
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1

EDIT: The above would not change the HTTP_HOST header that was sent by the browser (maybe that can be done as well). This implies that it would only work if the subdomains are represented on the file system as separate directories. So, as the .htaccess would then be placed in the directory holding the website for x.domain.com, the RewriteCond wouldn't even be required. Also, the directory for this x.domain.com subdomain would in fact not need any HTML content then; in the end all content would be served from the directory of another subdomain.

EDIT: As the above does not seem to work either, and yields endless rewrite loops even when adding [NS], maybe simply adding [L] helps here:

RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [NS,L]

Or maybe one can set an environment variable to stop the loop:

RewriteCond %{ENV:MY_VAR} !=1
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [E=MY_VAR:1]

But, for both [L] and [E]: I'm just guessing; I've never made mod_rewrite jump into the directory of another virtual host. I am not sure it can be done to start with.

Unfortunately, it's unclear how one would add a new subdomain. If one would just need to create a new directory with the name of the subdomain (without any use of some administrative tool) then the provider might be be using system wide rewriting as well. In fact, even without subdomains the provider might be doing some Mass Virtual Hosting as described in the URL Rewrite Guide.

I guess the best solution would be to change the value of HTTP_HOST on the fly, to solve issues with any system wide rewriting. Maybe the following is allowed to achieve that:

RewriteCond %{HTTP_HOST} ^x.domain.com$
RewriteRule ^(.*)$ /path/to/y.domain.folder/$1 [E=HTTP_HOST:y.domain.com]

Again, as the above would only be present in the .htaccess in the x.domain.folder, the RewriteCond is probably not needed at all.

Arjan
the latter gives me a 500 internal server error :( ...
jacob
And what does the error log say? Do you know how your virtual hosts are set up? (Do you somehow need to "activate" new subdomains, or do you simply create a new folder?) I guess you don't even need the RewriteCond, when this .htaccess in the folder for x.domain.com
Arjan
(The subdomains are activated afaik, that is, I can access them as such.)some things work, some don't :\this DOESN't work:500 error:RewriteRule ^(.*)$ /path/to/y.domain.folder/$1404 error:RewriteCond %{REQUEST_FILENAME} ^/path/to/x.domain.folder/$RewriteRule ^(.*)$ /path/to/y.domain.folder/$1This WORKS!RewriteCond %{REQUEST_FILENAME} ^/path/to/x.domain.folder/$RewriteRule ^(.*)$ /path/to/x.domain.folder/$1
jacob
Surely the error logs would give more than just the codes? It seems that you're not allowed to fetch files from a folder of a different subdomain/virtual host. Unless you find a solution for that (which I doubt, as you cannot configure Apache and don't know how the subdomains have been configured) then you'd need the Proxy flag (or ProxyPass directive). But that probably has not been enabled in Apache either...
Arjan
I am sorry for my unawareness. Thank you for the time you spent trying to help me :) ... I'll mark your answer as correct.
jacob
Hoho, don't give up! I'm not here for the reputation, so: no details in the logs? (Can you access the logs to start with?)
Arjan
If the subdomains have somehow been set up using symbolic links, then maybe adding "Options +FollowSymlinks" to the second answer helps
Arjan
I'm here again. I'll let you know, as soon as I know more. thanks for helping me :)
jacob
I put the comment in the original question.
jacob
I'll go ask the sysadmin to figure out what's wrong... (it is still redirecting endelessly even with [E=HTTP_HOST:y.domain.com])
jacob
I'm not claiming that it SHOULD work... ;-) So someone at work does in fact control that server?
Arjan
+1  A: 

Assuming you have access to the Apache configuration, create the following virtual host for domain x.domain.com. Then simply update y to whatever you need each season.

<VirtualHost ...:80>
  ServerName x.domain.com

  UseCanonicalName Off
  ProxyRequests Off

  <Proxy *>
          Order Allow,Deny
          Allow from all
  </Proxy>

  ProxyPreserveHost Off

  RewriteEngine On
  RewriteRule      ^$             http://y.domain.com/ [P,NC]
  RewriteRule      ^/(.*)$        http://y.domain.com/$1 [P,NC]
  ProxyPassReverse /              http://y.domain.com/
</VirtualHost>

Also to pick up the Alias suggestions, if you have multiple virtual hosts (one for each season) then you could put a server alias into the current domain. E.g.

<VirtualHost ...:80>
  ServerName summer.domain.com
  ServerAlias x.domain.com
  ...
</VirtualHost>

<VirtualHost ...:80>
  ServerName spring.domain.com
  ...
</VirtualHost>

...

This would make Apache deliver the summer.domain.com pages if you go to x.domain.com. If your seasonal subdomains depend on the HOST header line to be set correctly (i.e. to season.domain.com) you would need to use the first suggestion above, though.

dseifert
Unfortunatly I have no access to the Apache configuration ATM, but it will surely help as soon as I have. Thanks!
jacob
At least for the ServerAlias directive there is a chance that the webserver configuration frontend I presume you are using offers to set this option somehow.
dseifert