代码之家  ›  专栏  ›  技术社区  ›  Adam Harte

从子文件夹托管主域

  •  4
  • Adam Harte  · 技术社区  · 15 年前

    我有一个问题,使一个子目录作为我的主域的公共\uhtml,并获得一个解决方案,与该领域的子目录也工作。

    背景

    ~/public\u html/ 调用的目录 /域/ ,其中我为每个单独的网站创建一个文件夹。我的服务器上的文件夹结构如下所示:

        • 网站二
        • 网站三

    这使我的网站保持整洁。唯一的问题是让我的“主域”适应这个系统。我的主域似乎与我的帐户(或Apache之类的)绑定,所以我不能更改这个域的“文档根目录”。我可以为我在cPanel中添加的任何其他域(“附加域”)定义文档根。但主要领域是不同的。

    问题

    我被告知编辑.htaccess文件,将主域重定向到子目录。这似乎工作得很好,我的网站在主页/索引页上工作得很好。

    我遇到的问题是,如果我尝试在浏览器中导航到主站点的images文件夹(仅举个例子),如下所示:

    www.yourmaindomain.com/images/

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

    它实际上仍然显示了正确的“Index of/images”页面,并显示了我所有图像的列表。只是“漂亮”的网址才是问题所在。

    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 回复  |  直到 15 年前
        1
  •  1
  •   Marian    15 年前

    不要用mod\u重写,用 virtual hosts 为了这个。我不会在这里详细解释,你可以在上面的链接中找到所有必要的信息(提示:您可能需要基于名称的。)

    ~/public_html/ 去一些更普通的地方,比如 /var/www/ /srv/www .

    Don't use .htaccess files on production .

        2
  •  0
  •   Bernard    9 年前

    这个.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]