0

我尝试阅读许多有关 htaccess 的答案,但找不到出路。在 webhosting 中对该架构进行映像:root/public_html/subfolder 现在使用 htaccess 文件中的以下代码(放置在 public_html 中)我可以管理,当在浏览器中键入域时,访问者会进入没有数字 example.com/subfolder 的子文件夹,例如。 com就够了。有用。我现在想要的是,访问者不会被重定向到子文件夹中,而是在放置在根目录内的“额外文件夹”中(级别根目录与放置 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/ 更改 /subfolder/ (假设双点会向上推一级)但不起作用。任何想法?谢谢

4

1 回答 1

0

您放置的当前配置仅在您将 .htaccess 放入网站的根文件夹(在本例中为 public_html)时才有效,因此除非您有目录别名,否则无法访问该根目录之外的另一个文件夹。

如果您有权更改您的 apache 配置

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

1- 你的 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-您需要将此别名/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访问它

于 2016-06-07T22:21:41.267 回答