.htaccess for Pan-Domain Pointing Subdirectory
Howto make this with htacess:
a.domain.com -> domain.com/a
b.domain.com -> d开发者_JS百科omain.com/b
.
.
.
z.domain.com -> domain.com/z
i try :
RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]
but 500 error
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L,QSA]
Possible Problems:
- Did you set the line
RewriteEngine on
above your code? - Is
mod_rewrite
installed/enabled?
Very similar to: mod_rewrite is ignoring rules in subdirectories
Unfortunately, the rules you have got will not work (to be precise: they actually work -- URL got rewritten .. but then after Apache sees [L]
flag it goes to another iteration .. where it gets rewritten again .. and again -- entering endless loop which Apache has to break at some point).
You have to add some condition which will break the iteration completely. There few possible ways of doing it (depends on the rest of your rules, you can read a bit more here -- performance impact of order of rewrite rules when using apache mod_rewrite ).
I have modified the rule by adding 2 more conditions which makes it to rewrite ONLY IF destination DOES EXIST.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule (.*) %1/$1 [L,QSA]
So .. how it works. We will use this URL as an example: http://home.domain.com/kaboom.txt
.
- If file
domain.com/home/kaboom.txt
DOES NOT exist -- nothing happends. - If such file DOES exist -- URL gets internally rewritten to
http://home.domain.com/home/kaboom.txt
(the full URL). Apache goes to next iteration. There it checks doesdomain.com/home/home/kaboom.txt
exists. Most likely not (unless you have subfolders with the same name as sub-domain) and no more rewriting occurs -- job is done.
精彩评论