【问题标题】:Rewriting from public_html to root从 public_html 重写为 root
【发布时间】:2016-06-06 09:33:16
【问题描述】:

我尝试阅读许多有关 htaccess 的答案,但找不到出路。 在虚拟主机中映像此架构:root/public_html/subfolder 现在使用 htaccess 文件中的以下代码(放置在 public_html 中)我可以管理,当在浏览器中键入域时,访问者会进入没有数字 example.com/subfolder 的子文件夹,example.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/ (假设双点会向上推一级)但不起作用。 任何想法? 谢谢

【问题讨论】:

  • 这应该是2部分的buzzle游戏,你只是把它的第二部分.htaccess配置,但第一部分是apache站点配置虚拟主机,你可以在/etc/apache2中找到它/sites-enabled/ 如果您使用的是基于 Linux 的系统。如果您需要明确的答案可能对您有所帮助,您也需要放置 apache 配置。
  • 谢谢你回答我。我检查但我没有那个文件/文件夹等。我最终可以创建一个 Apache 处理程序。还有其他想法吗?
  • 好的,我会在答案中设置它。

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


【解决方案1】:

如果您将 .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 访问它

【讨论】:

  • 谢谢,我发现我无法访问 apache 虚拟主机配置,所以我唯一能做的就是完全反转整个托管:将文件夹从根目录移动到公共,然后将文件夹移动到公开到根目录,所以我会让访问者登陆我想要的文件夹......我想不出更好的东西。无论如何,任何最后一分钟的想法都值得赞赏。
  • 抱歉,我试图为您找到一种在 cPanel 之类的东西中创建目录别名的方法,但我找不到。
猜你喜欢
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 2013-05-11
  • 2018-08-08
相关资源
最近更新 更多