imran
imran

Reputation: 179

Handle dynamic Subdomains in .htaccess

I am trying to create custom domain names using .htacess

Here is what i am doing

http://username.mydomain.com is accessing http://mydomain.com/live/agent/index.php?agent_user_name=username (working fine)

here is .htaccess

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]

RewriteCond %1 !^www$ [NC]

RewriteRule ^$ /live/agent/index.php?agent_user_name=%1 [L]

But now I want to handle other pages as well

http://username.mydomain.com/blog should access http://mydomain.com/live/agent/blog.php?agent_user_name=username

Please help how to handle this as well in .htaccess

Upvotes: 1

Views: 306

Answers (1)

anubhava
anubhava

Reputation: 785128

You can have this additional rule:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]

RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]

Upvotes: 1

Related Questions