.htaccess - Redirect subdomain to folder

僤鯓⒐⒋嵵緔 提交于 2019-11-27 04:27:04

I have set CNAME of my sub domain below:

blog.mydomain.com

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

Formerly I need to use this url to call wordpress:

http://blog.mydomain.com/blog/

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

http://blog.mydomain.com/

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: http://forum.mydomain.com 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.

Add this to your .htaccess in your web root / directory

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]

The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.

Latheesan

Try this example:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]

Any requests http://test.example.com will be mapped to http://example.com/test/...

Try googling dynamic subdomain with php and htaccess to get better search results.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!