xmxmxmx
xmxmxmx

Reputation: 439

Htaccess multiple redirect rules should be working

This should be working according to htaccess tester http://htaccess.madewithlove.be/

I'm trying to get url's in the format subdomain.domain.com to resolve to domain.com/index.php?sub=subdomain

However I've also want any links in the form subdomain.domain.com/pagename to redirect to domain.com/index.php?tpl=page&sub=subdomain&url=pagename

At the moment the first rule works if i remove the second rule but if I include both only the second rule works.

Here's the full htaccess

RewriteEngine On

#EDIT: this was messing it all up by appending index.html so the subdomain only 
# wasn't triggering at all due to appended pagename
DirectoryIndex disabled


#Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/index.php?sub=%1 [P,NC,QSA,L]

#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteRule ^(.+/)?([^/]*)$ http://example.com/index.php?tpl=page&sub=%1&url=$2 [P,NC,QSA,L]

If you answer this I will make you Secretary of State once Flat Earth goes mainstream. Thanks!

Upvotes: 1

Views: 229

Answers (1)

anubhava
anubhava

Reputation: 784898

Have it like this:

DirectoryIndex disabled
RewriteEngine On

#Remove www
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ index.php [L]

#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^index\.php$ http://%2/index.php?sub=%1 [P,QSA,NC,L]

#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^(?:.+/)?([^/]+)/?$ http://%2/index.php?tpl=page&sub=%1&url=$1 [P,QSA,L]

Upvotes: 1

Related Questions