从public_html重写为root

时间:2016-06-06 09:33:17

标签: .htaccess mod-rewrite url-rewriting document-root public-html

我尝试阅读有关htaccess的许多答案,但我找不到出路。 在webhosting中构建此体系结构:root / public_html / subfolder 现在使用htaccess文件中的以下代码(放在public_html中)我可以管理当域名在浏览器中输入时,访问者登陆子文件夹而没有数字example.com/subfolder,example.com就足够了。有用。 我现在想要的是访问者不会重定向到子文件夹中,而是重定向到" extrafolder"放置在根目录内(与根文件放置在同一级别,而文件夹为public_html,但不在public_html中)。 那么如何修改这段代码:

# .htaccess main domain to subdirectory redirect
# Do not change this line.
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} !^/subfolder/
# 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 ^(.*)$ /subfolder/$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 ^(/)?$ /subfolder/index.html [L] 

我尝试使用../extrafolder/更改/子文件夹/(假设双点将向上推一级)但不起作用。 任何的想法? 谢谢

1 个答案:

答案 0 :(得分:0)

您放置的当前配置只是将.htaccess放在您网站的根文件夹中,在这种情况下是public_html,因此无法访问该根目录以外的其他文件夹,除非您有目录别名。

如果您有权更改Apache配置

您需要执行以下步骤才能完全控制网站的根目录。

1- Yur apache虚拟主机配置(应该在/etc/apache2/sites-enabled/example.conf中)应该类似于以下内容:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/test
    <Directory /var/www/test/>
        DirectoryIndex index.php
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

3-您需要将此 Alias / externalfolder /“/ var / www / test / externalfolder /” 添加到上面的虚拟主机配置

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/test/public_html
    Alias /externalfolder/ "/var/www/test/externalfolder/"
    <Directory /var/www/test/public_html>
        DirectoryIndex index.php
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

现在您可以通过http://example.com/externalfolder/file.php

访问该内容了