htaccess rewrite only main domain to subfolder

匆匆过客 提交于 2019-12-24 11:51:47

问题


I'm using a cPanel account to host multiple sites. To have a clean public_html, I use .htaccess to redirect the main domain into one of the subfolders.

So when user types in www.example.com, it will be rewritten into www.example.com/mainsite/ but still showing www.example.com in the URL.

But when using the codes below, every domain (including sub domains) will get redirected to that folder:

RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)$ /mainsite/$1 [NC,L]

How can I target only www.example.com (and www.example.com/files) to be rewritten to the /mainsite?

*Other subdomains should be pointing to their own Document_Root as created.


回答1:


You need one more RewriteCond to check for main domain:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mainsite/ [NC]
RewriteRule ^(.*)$ /mainsite/$1 [L]


来源:https://stackoverflow.com/questions/45182490/htaccess-rewrite-only-main-domain-to-subfolder

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