首页 文章

从子文件夹中托管主域

提问于
浏览
4

我有一个问题,使子目录充当我的主域的public_html,并获得一个与该域子目录一起使用的解决方案 .

背景

我的托管允许我托管多个站点,这些站点都运行良好 . 我在 ~/public_html/ 目录下设置了一个名为 /domains/ 的子文件夹,我在其中为每个单独的网站创建一个文件夹 . 我服务器上的文件夹结构如下所示:

  • public_html

  • 个域名

  • websiteone

  • websitetwo

  • websitethree

  • ......

这使我的网站保持整洁 . 唯一的问题是让我的“主域”适合这个系统 . 它似乎是我的主要域,以某种方式绑定到我的帐户(或Apache,或其他东西),所以我无法更改此域的“文档根” . 我可以为cPanel中添加的任何其他域(“Addon Domains”)定义文档根目录没问题 . 但主要领域是不同的 .

问题

有人告诉我编辑.htaccess文件,将主域重定向到子目录 . 这似乎工作得很好,我的网站在它的主页/索引页面上工作正常 .

我遇到的问题是,如果我尝试浏览我的浏览器来说出我的主站点的图像文件夹(例如),就像这样:

www.yourmaindomain.com/images/

然后它似乎忽略了重定向并在url中显示整个服务器目录,如下所示:

www.yourmaindomain.com/domains/yourmaindomain/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文件看起来是否正确?我只需要这样做,所以我的主域行为就像一个插件域,它的子目录遵循重定向规则 .

2 回答

  • 1

    不要使用mod_rewrite,使用virtual hosts . 我赢了't explain that here in detail, you'将在上面的链接中找到所有必要的信息 . (提示:你可能需要基于名称的那些 . )

    你可能想把你的东西从 ~/public_html/ 移到一些更通用的地方,如 /var/www//srv/www .

    除此之外:Don't use .htaccess files on production .

  • 0

    我的组织方式和你做的一样 . 和我的主域名有同样的问题 . 这个.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]
    

相关问题