0

I have this code in .htaccess :

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$

# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$ 
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]

# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]

#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register  /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass  /index.php?sub=$1&page=lostpass
...

(a rule for wildcard subdmains is already in place and working)

If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register

What am I doing wrong here? Thanks in advance!

rtrudel
  • 21
  • 3

1 Answers1

0

Try adding the L flag to your second redirect rule, similar to how have it in the first.

RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]

It looks like your rewritten URI is passing through to the next rule.

Also, I don't think your first two RewriteCond are in the correct spot.

matthew
  • 2,832
  • 3
  • 24
  • 24
  • Thanks. I added the L flag, I guess it will just work better even if I don't notice any significant difference on usage. I'm new at this, where those RewriteCond should be? – rtrudel Jan 08 '13 at 20:43
  • The RewriteConds should be before the rule they apply to. As is they only apply to the first rule, and would appear to be unnecessary. Typically you would see those conditions before a general catch all rule that forwards requests to a script. – matthew Jan 08 '13 at 22:21