Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Subdomain to domain re-writing


jdMorgan - 5:27 pm on Aug 25, 2010 (gmt 0)


Well, that was not clear, and I assumed that you already had the subdomain-URL to subdomain-subdirectory-filepath rewrite in place.

Also, your examples appear to be inconsistent: You're using "subdomain" in one place, and "/features" in another. I will assume here that the subdomain is "subdomain.example.com" and the the directory is called "/features." If this is wrong, that is OK, because you can easily edit the subdomain hostname or subdomain-subdirectory name if I use separate names for them in this code, while the required modification to go the other way wouldn't be clear at all if I used the same name.

You need a two-rule set: One rule to externally redirect direct client requests for the subdomain's subdirectory filepath as a URL back to the correct/canonical subdomain root URL, and the other rule to internally rewrite requests for the canonical subdomain URLs to the subdomain's subdirectory filepath inside the server. This first rule is what I already posted above.

This code assumes that both your main domain and your subdomain resolve to your top-level file directory. If you used a "Control Panel" to "define" your subdomain, then this may not be the case, and this code may not be able to work. In fact, on some server's where a control panel is used to define subdomains, you cannot refer to any directory "above" the subdomain's subdirectory. In this case, you can use only the first rule, located in the /.htaccess file.

Here's the rwo-rule set:

# Externally redirect direct client requests for subdomain-subdirectory
# URLs back to canonical subdomain root URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /features(/[^\ ]*)?\ HTTP/
RewriteRule ^features(/(.*))?$ http://subdomain.example.com/$2 [R=301,L]
#
# Internally rewrite requests for subdomain.example.com URLs to
# the /features filepath, unless this has already been done
RewriteCond $1 !^features/
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ /features/$1 [L]

Jim


Thread source:: http://www.webmasterworld.com/apache/4191662.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com