Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need to redirect all the traffic from

http://subdomain.domain.com/

to

http://subdomain.domain.com/folder/

With 'folder' being a specific folder, of course. I've tried this:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^.*$ http://subdomain.domain.com/folder/ [L]

But it doesn't work. What's wrong?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Try this:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^/$ http://subdomain.domain.com/folder/ [R,L]

The only change I made was to add R in the options after the RewriteRule. That option tells Apache to do a 302 redirect. You can use R=301 if you want a permanent redirect.

share|improve this answer
    
The URL is redirected, but it gives the following error: "Error 310 (net::ERR_TOO_MANY_REDIRECTS)" in Chrome under Mac OS X. The page is blank, apart from that error :( –  javipas Nov 21 '11 at 15:48
1  
I have edited my answer. The first part of the RewriteRule is now ^/$ so that it doesn't end up in a redirect loop. I'm surprised I didn't notice that when I first answered. –  Ladadadada Nov 21 '11 at 15:53
    
That did it. Would you be so kind to explain me why it was wrong with the past version? Thanks! –  javipas Nov 21 '11 at 18:57
    
A RewriteRule is made up of three parts separated by spaces: the pattern, the substitute and the options. The pattern is a regex that is matched against the request URI. In the first version, the pattern was ^.*$ which as a regex matches any URI. Any URI you requested would always redirect to /folder/, even if you requested /folder/, it would still send a redirect response pointing to /folder/. In the second version, it now only matches exactly /. The Apache rewrite guide is highly recommended. –  Ladadadada Nov 21 '11 at 19:39
    
Fantastic explanation, I'll read the rewrite guide, thanks for the comment (I've written this while singing your name :) ) –  javipas Nov 22 '11 at 11:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.