从子文件夹托管主域
我在使子目录充当我的主域的 public_html 时遇到问题,并获得也适用于该域子目录的解决方案。
背景
我的托管允许我托管多个站点,这些站点都运行良好。我在 ~/public_html/ 目录下设置了一个名为 /domains/ 的子文件夹,在其中为每个单独的网站创建一个文件夹。我的服务器上的文件夹结构如下所示:
- public_html
- 域名
- 网站一
- 网站二
- 网站三
- ...
- 域名
这使我的网站保持整洁。唯一的问题是让我的“主域”适应这个系统。看来我的主域以某种方式与我的帐户(或 Apache 或其他东西)相关联,所以我无法更改该域的“文档根”。我可以定义我在 cPanel 中添加的任何其他域(“附加域”)的文档根,没有问题。但主域不同。
问题
我被告知编辑 .htaccess 文件,将主域重定向到子目录。这似乎工作得很好,我的网站在其主页/索引页面上运行良好。
我遇到的问题是,如果我尝试导航浏览器以说出我的主站点的图像文件夹(仅作为示例),如下所示:
www.yourmaindomain.com/images/
然后它似乎忽略了重定向并在 url 中显示整个服务器目录,如下所示:
www.yourmaindomain.com/domains/yourmaindomain/images/
它实际上仍然显示正确的“/images 索引”页面,并显示我所有图像的列表。只是“漂亮”的 URL 才是问题所在。
我的 .htaccess 示例
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteCond %{REQUEST_URI} !^/domains/yourmaindomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domains/yourmaindomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ domains/yourmaindomain/index.html [L]
此 htaccess 文件看起来正确吗?我只需要让我的主域表现得像一个插件域,并且它的子目录遵守重定向规则。
I am having a problem making a sub directory act as the public_html for my main domain, and getting a solution that works with that domains sub directories too.
Background
My hosting allows me to host multiple sites, which are all working great. I have set up a subfolder under my ~/public_html/ directory called /domains/, where I create a folder for each separate website. The folder structure on my server looks something like this:
- public_html
- domains
- websiteone
- websitetwo
- websitethree
- ...
- domains
This keeps my sites nice and tidy. The only issue was getting my "main domain" to fit into this system. It seems my main domain, is somehow tied to my account (or to Apache, or something), so I can't change the "document root" of this domain. I can define the document roots for any other domains ("Addon Domains") that I add in cPanel no problem. But the main domain is different.
The problem
I was told to edit the .htaccess file, to redirect the main domain to a subdirectory. This seemed to work great, and my site works fine on it's home/index page.
The problem I'm having is that if I try to navigate my browser to say the images folder (just for example) of my main site, like this:
www.yourmaindomain.com/images/
then it seems to ignore the redirect and shows the entire server directory in the url, like this:
www.yourmaindomain.com/domains/yourmaindomain/images/
It still actually shows the correct "Index of /images" page, and shows the list of all my images. It's just the "pretty" URL that's the problem.
Example of my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteCond %{REQUEST_URI} !^/domains/yourmaindomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domains/yourmaindomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ domains/yourmaindomain/index.html [L]
Does this htaccess file look correct? I just need to make it so my main domain behaves like an addon domain, and it's subdirectories adhere to the redirect rules.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 mod_rewrite 来实现这一点,而使用 虚拟主机 来实现这一点。我不会在这里详细解释,您可以在上面的链接中找到所有必要的信息。 (提示:您可能需要基于名称的内容。)
您可能希望将内容从
~/public_html/
移至一些更通用的位置,例如/var/www/< /code> 或
/srv/www
。除此之外: 不要使用
.htaccess
生产文件。Don't use mod_rewrite for that, use virtual hosts for that. I won't explain that here in detail, you'll find all the necessary information at the above link. (Hint: You'll need the name-based ones probably.)
You probably want to move your stuff out of
~/public_html/
to some more generic place like/var/www/
or/srv/www
.Besides that: Don't use
.htaccess
files on production.我按照你的方式组织它。我的主域也有同样的问题。
这个 .htaccess 对我有用,甚至解决了丑陋的 URL 问题。
I have it organized the same way yo do. Had the same issue with my main domain.
This .htaccess works for me and even solves the ugly URL thing.