Isaiah Isaiah - 4 years ago 322
PHP Question

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.

I simply want
to be redirected to
without the user seeing the URL change.

So if
is requested, I want the user to keep seeing this URL, while the page given is actually

Note: I do not want
, so simply

Thanks in advance,

Isaiah v. Hunen

Answer Source

I have set CNAME of my sub domain below:

to my wordpress that installed in folder /blog/ under root directory.

Formerly I need to use this url to call wordpress:

which is ugly. I have tried many code to redirect:

to the folder so I can use it as my wordpress url.

Finally I got .htaccess setting that is work:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteRule (.*) /blog/$1

I have also CNAME other subdomain: to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
    RewriteCond %{REQUEST_URI} !^/forum/mybb/
    RewriteRule (.*) /forum/mybb/$1 [L]

    RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteRule (.*) /blog/$1 [L]

In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download