Forum Moderators: phranque
This is not a previously answered question! I have looked around quite a bit but could not find a similar question asked or answered.
Here is my problem. I am trying to host subdomains using a .htaccess file and mod_rewrite. Each of the subdomains have their own directories which are named after the subdomain itself. Here is what the relavant lines from my .htaccess file look like:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!^(www\.)?domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*\.domain\.com)$ [NC]
RewriteRule (.*) /%1/$1 [L]
This works fine when a request for sub.domain.com comes through. The problem is this: if I click on a link in index.html under sub.domain.com that points to another subdirectory called photos, the browser URL line shows sub.domain.com/sub.domain.com/photos. That looks ugly - what I would like very much is for it to show sub.domain.com/photos instead.
I will greatly appreciate any help. I think the problem is recursion of some kind, but I am not at all sure. Or maybe the webserver is passing the whole url (sub.domain.com/photos) to RewriteRule?
ANY help/pointers are appreciated - I have been doing trial/error type stuff for the past two days and am at my wits end.
Thanks in advance.
Welcome to WebmasterWorld!
Actually, this subject has been addressed before. See this thread on Rewriting arbitrary subdomains to subdirectories [webmasterworld.com]. The code posted there in msg#6 takes care of the recursion problem.
Jim
Thanks for your reply. I have tried the code you posted in the thread you are referring to. It works for [sub.domain.com...] and takes me to /www.domain.com/sub.domain.com/index.html on the filesystem.
However, my sub.domain.com/index.html has a link that reads:
<a href="photos">photos</a>. When I click on this link, the url bar in my browser reads [sub.domain.com...] Whereas I want it to read [sub.domain.com...]
I other think I forgot to mention is that if I manually type [sub.domain.com...] into the URL bar, it works fine. It's just the links that are misbehaving.
I am sorry if I sound like I am repeating myself - I was not sure if my original posting was descriptive enough.
Here is a copy of the relavant lines from my current .htaccess file, after making the changes you suggested:
RewriteEngine On
RewriteBase /
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path
# element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+\.domain\.com)(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path
# element (prevents mod_rewrite recursion)
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]
Then it's not likely to be a mod_rewrite problem, because as far as the server is concerned, it is just serving whatever the browser asked for. It won't try to serve an incorrect file unless the browser asks for an incorrect URL. It is very likely the browser that is incorrectly resolving the canonical address from the relative link on your page. So look to the form of the on-page links themselves.
As an experiment to get more information, try "<a href="photos">/photos</a>" in one of your on-page links and see if that changes anything. Also, check that you don't have a "link rel" or "URL base" tag in your html that is conflicting with the rewrite code. Two other things to check are whether your server configuration has UseCanonicalName on set, and whether content-negotiation is enabled. If either of these are set and you don't need them, they can cause problems.
Even simpler, you might want to do a View-Page Source in your browser to make sure those on-page links are actually in the form you expect them to be. And check the status bar in your browser when you hover over a link and see what it says. Just look for URL inconsistencies.
This is a weird problem, so it's going to take some digging to figure it out.
Jim
phew, I think I have it figured out now. A variation of what you suggested - <a href="photos/"> does it and takes me to sub.domain.com/photos/ which works great for me.
I still do not understand why <a href="photos"> takes me to sub.domain.com/sub.domain.com/photos. While it would be nice to know, I am perfectly happy with the currently solution.
Thanks a lot for your help. This fourm is great and has a very high SNR :)