从子文件夹中托管主域

时间:2010-06-07 20:28:14

标签: apache .htaccess sysadmin shared-hosting cpanel

我遇到问题,使子目录充当我的主域的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 个答案:

答案 0 :(得分:1)

不要使用mod_rewrite,为此使用virtual hosts。我不会在这里详细解释,您可以在上面的链接中找到所有必要的信息。 (提示:你可能需要基于名称的那些。)

您可能希望将您的内容从~/public_html/移到某个更通用的位置,例如/var/www//srv/www

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

答案 1 :(得分: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]
相关问题