Using .htaccess to rewrite dynamic subdomains

Posted by brokekidweb on Stack Overflow See other posts from Stack Overflow or by brokekidweb
Published on 2013-11-08T03:18:18Z Indexed on 2013/11/08 3:53 UTC
Read the original article Hit count: 167

Filed under:
|
|
|
|

I'm currently using .htaccess to rewrite on my website to create dynamic subdomains. I also use it to remove the .php extension on all pages. However, once inside the subdomain, it tries to redirect again. For example, if you went to https://admin.example.com/test, it would actually be accessing https://example.com/clients/admin/test.php. I keeping getting various 404 errors using the following .htaccess file:

Options +MultiViews
Options +FollowSymLinks

RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]

RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]

How can I keep this from redirecting to https://admin.example.com/clients/admin/test.php?

© Stack Overflow or respective owner

Related posts about php

Related posts about apache