从子文件夹托管主域

发布于 2024-09-04 10:00:56 字数 1350 浏览 9 评论 0原文

我在使子目录充当我的主域的 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
      • ...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-09-11 10:00:56

不要使用 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.

橘寄 2024-09-11 10:00:56

我按照你的方式组织它。我的主域也有同样的问题。
这个 .htaccess 对我有用,甚至解决了丑陋的 URL 问题。

# .htaccess main domain to subdirectory redirect 

RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/subdirectory/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /subdirectory/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ subdirectory/ [L]

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.

# .htaccess main domain to subdirectory redirect 

RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/subdirectory/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /subdirectory/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ subdirectory/ [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文