ozgrozer
ozgrozer

Reputation: 2042

.htaccess catch all subdomains & GET as GET variable

The following examples explain my problem.

This is my .htaccess code

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /user.php?user=%1

RewriteRule ^user\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /user.php?path=$1 [L,QSA]

Expected:

http://example.com                      > /index.php
http://example.com/contact              > /index.php?path=contact
http://example.com/cat/sub1/sub2        > /index.php?path=cat/sub1/sub2
http://jack.example.com                 > /user.php?user=jack
http://jack.example.com/contact         > /user.php?user=jack&path=contact
http://jack.example.com/cat/sub1/sub2   > /user.php?user=jack&path=cat/sub1/sub2

Happened:

http://example.com                      > OK
http://example.com/contact              > It opens user.php?path=contact
http://example.com/cat/sub1/sub2        > It opens user.php?path=cat/sub1/sub2
http://jack.example.com                 > OK
http://jack.example.com/contact         > OK
http://jack.example.com/cat/sub1/sub2   > OK

I tried lots of way but didn't solve it. Any help?

Upvotes: 2

Views: 238

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

Since you are using the same main domain, I would just include it in the rules instead of wildcards. Give this a try.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} ^/$    
RewriteCond %{HTTP_HOST} ((?!www).+)\.example\.com [NC]
RewriteRule ^$ /user.php?user=%1 [L]

RewriteCond %{HTTP_HOST} ((?!www).+)\.example\.com [NC]
RewriteRule ^(.+)$ /user.php?user=%1&path=$1 [L]

RewriteRule ^user\.php$ - [L]

RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]

Upvotes: 2

Related Questions