tags:

views:

126

answers:

2

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."

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]
A: 

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.

Brent Kirby
Thanks, Brent!Unfortunately it does not work.Apache says there is a syntax error in RewriteBase line.RewriteBase / # Make sure it starts from the domainYes I write it without the space between Rewrite and Base, but it does not help. Still have the error.
Pavel
A: 

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

John Weldon
John, I have changed the script the way you propose, but in this case the browser redirects to example.com instead of example.com/fishSee the full script in my update to the topic.
Pavel