在我的本地机器上,以下工作完美:
<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host2.localhost
DocumentRoot "C:/xampp/htdocs/website_1"
ServerName testpage.com/website_1
##ServerAlias www.recruitement.localhost
##ErrorLog "logs/dummy-host2.localhost-error.log"
##CustomLog "logs/dummy-host2.localhost-access.log" combined
</VirtualHost>
但是我将我的网站托管到名为 justhost.com 的托管公司,他们不允许我修改 httpd-vhosts.conf 或 httpd.conf强>。现在我的整个站点都经过编码,以便 website_1 下的文件使用简单的斜线“/”引用 website_1 下的其他文件,这意味着 website_1 被视为文档根目录。这在本地机器上完美运行,但当上传到主机时,它给我服务器错误,因为无法找到文件,因为它试图在 public_html
中找到这些文件例如:
public_html
- website_1
- script.php
- style.css
在我的 script.php 中:
<a href="/style.css">See My Style</a>
这在本地机器上运行良好,但在主机上失败,因为它试图在 public_html 而不是 public_html/website_1
下找到 style.css有没有办法不使用虚拟主机就拥有多个文档根目录?就像使用 .htaccess 或其他东西一样。我想尽量避免重写代码,因为它有大约 10,000 行代码。
最佳答案
通过 httpd.conf
启用 mod_rewrite 和 .htaccess,然后将此代码放入 DOCUMENT_ROOT
目录下的 .htaccess
中:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/website_1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/website_1/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/website_1/$1 -l
RewriteRule (?!^website_1/)^(.*)$ /website_1/$1 [R=302,L,NC]
一旦您确认它工作正常,请将 R=302
替换为 R=301
。在测试您的 mod_rewrite 规则时避免使用 R=301
(永久重定向)。
关于php - 如何使用 .htaccess 而不是 VHost 将文档根目录设置为子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512881/