开发者

Why subdomain rewrite does not work?

I want my subdomain to point directly to a folder. I have found the following mod_rewrite script to setup this behavior. Unfortunately it does not work.

When I navigate to fish.example.com, the browser displays 404 error with the following message. "The requested URL / was not found on this server."

开发者_如何学Go

Do you know why? How can I make it work?

    # Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteEngine on

RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /fish/%1/$1 [L]

UPDATE

I have changed the script to the following, but in this case the browser redirects to example.com instead of example.com/fish Do you know why?

RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^fish/(.*) /fish/%1/$1 [L]


Try this:

RewriteEngine On

Rewrite Base / # Make sure it starts from the domain

RewriteCond %{HTTP_HOST} ^fish.example.com [NC] # Catch your subdomain

RewriteRule (.*)$ fish/$1 [L] # Reroute to folder.


Update:

I see... try this:

RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) http://example.com/%1/$1 [R=301,L]

Basically a 301 redirect... %1 matches the subdomain in the previous RewriteCond, and $1 matches the original url

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜