I have a redirect problem with 'RewriteRule', i have this in mt .htaccess:

RewriteCond %{HTTP_HOST} ^foo.domain.com
RewriteRule ^(.*)$ http://www.domain.com/foo/$1 [R=permanent,L]

If i go to foo.domain.com redirect to www.domain.com/foo/ and it's ok, instead i go to foo.domain.com/bar i have an error, must go to www.domain.com/foo/bar. Where is error?

  • Are you trying to run the site from ./foo folder? – Raza Oct 3 '14 at 18:10
  • Can you do something like use Firefox's "Live HTTP Headers" add-on, and watch the redirection take place? With HTTP, just looking at a log file sometimes doesn't give you the whole picture. – Bruce Ediger Oct 3 '14 at 18:10
  • With "Live HHTP Headers" the next redirect after foo.domain.com/bar is www.domain.com/fooindex.php – hellb0y77 Oct 4 '14 at 19:12

Apache (or mod_rewrite) might be constructing the "Location:" header line wrong. Add a slash to the rewrite rule:

RewriteCond %{HTTP_HOST} ^foo.domain.com
RewriteRule ^/(.*)$ http://www.domain.com/foo/$1 [R=permanent,L]

When I tried this without the slash, I would get "Location:" header lines that looked something like: http://www.stratigery.com/foo//whatever. I'm not sure that all browsers can handle an extra slash in the URL.

  • Not work, with / redirect to www.domain.com – hellb0y77 Oct 4 '14 at 19:13
  • Nothing? Any ideas? – hellb0y77 Oct 7 '14 at 19:10
  • @hellb0y77 - turn on mod_rewrite logging and see what happens. The directive is: RewriteLogLevel 3, and you probably want to specify the log file with: RewriteLog /var/tmp/rewrite.log or some directive like that. The log file output isn't terrifically natural, but you might be able to puzzle it out. – Bruce Ediger Oct 8 '14 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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