htaccess rewriting subdomain to php page

91 Views Asked by At

I don't have experience in htaccess and failed to find solution for that.

Simply, i want to make

something1.domain.com to open www.domain.com/something1.php
something2.domain.com to open www.domain.com/something2.php

& www.domain.com open www.domain.com/index.php

Thanks

2

There are 2 best solutions below

0
On

Place this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.domain\.com$ [NC]
RewriteRule ^/?$ http://www.domain.com/%1.php [L,R=302]
4
On

Here is one way of doing it -

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\.
RewriteCond %{HTTP_HOST} (.+)\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/%1.php [L]

I am assuming you have allowed overrides using AllowOverride directive in apache configuration. You have to enable mod_rewrite if the redirect has to be transparent. If you do not want that then remove the 'L' flag. The L flag prevents further processing of rules and immediately applies the rule that matched.