htaccess仅将主域重写为子文件夹 [英] htaccess rewrite only main domain to subfolder

查看:73
本文介绍了htaccess仅将主域重写为子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cPanel帐户托管多个网站.为了获得干净的public_html,我使用.htaccess将主域重定向到子文件夹之一.

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.

因此,当用户键入 www.example.com 时,它将被重写为 www.example.com/mainsite/,但仍显示 www.example网址中的.com .

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]

如何仅定位 www.example.com (和 www.example.com/files )以重写为/主站点?

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

*其他子域应指向创建时自己的Document_Root.

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

推荐答案

您还需要一个RewriteCond来检查主域:

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]

这篇关于htaccess仅将主域重写为子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆