php 如何使用 .htaccess 而不是 VHost 将文档根目录设置为子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16512881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:16:00  来源:igfitidea点击:

How to set document root to be a subdirectory using .htaccess and not VHost

phpapache.htaccessvhosts

提问by GGio

On my local machine the following works perfect:

在我的本地机器上,以下工作完美:

 <VirtualHost *:80>
       ##ServerAdmin [email protected]
       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>

Howerver Im hosting my website to hosting company called justhost.comand they do not allow me to modify httpd-vhosts.confor httpd.conf. Now my entire site is coded so that files under website_1 reference to other files under website_1 using simple slash "/" meaning website_1 is treated as document root. This works perfectly on local machine but when uploaded to host it gives me server errors because cant find the files since its trying to locate those files in public_html

但是,我将我的网站托管到名为justhost.com 的托管公司,他们不允许我修改httpd-vhosts.confhttpd.conf。现在我的整个站点都经过编码,以便 website_1 下的文件使用简单的斜杠“/”引用 website_1 下的其他文件,这意味着 website_1 被视为文档根目录。这在本地机器上完美运行,但是当上传到主机时,它会给我服务器错误,因为无法找到文件,因为它试图在public_html 中找到这些文件

For example:

例如:

  public_html
      - website_1
         - script.php
         - style.css

inside my script.php:

在我的script.php 中

<a href="/style.css">See My Style</a>

this works good on local machine but on host it fails since it tries to locate style.css under public_htmland not public_html/website_1

这在本地机器上运行良好,但在主机上它失败了,因为它试图在public_html而不是public_html/website_1下定位 style.css

Is there a way to have multiple document roots without using VHosts? Like using .htaccess or something else. Please I want to try to avoid rewriting the code as much as possible since its around 10 thousands lines of code.

有没有办法在不使用 VHosts 的情况下拥有多个文档根?就像使用 .htaccess 或其他东西一样。请我尽量避免重写代码,因为它大约有 1 万行代码。

回答by anubhava

Enable mod_rewrite and .htaccess through httpd.confand then put this code in your .htaccessunder DOCUMENT_ROOTdirectory:

通过启用 mod_rewrite 和 .htaccess httpd.conf,然后将此代码放在您的.htaccessDOCUMENT_ROOT目录中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/website_1/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/website_1/ -d [OR]
RewriteCond %{DOCUMENT_ROOT}/website_1/ -l
RewriteRule (?!^website_1/)^(.*)$ /website_1/ [R=302,L,NC]

Once you verify it is working fine, replace R=302to R=301. Avoid using R=301(Permanent Redirect) while testing your mod_rewrite rules.

一旦您确认它工作正常,请替换R=302R=301R=301在测试 mod_rewrite 规则时避免使用(Permanent Redirect)。

回答by Aman Varshney

Make a entry in a .htaccess. Suppose you have a website abc.com. You want to run another website in directory. Then create a .htaccess in parent directory pointed to abc.com

在 .htaccess 中创建一个条目。假设您有一个网站 abc.com。您想在目录中运行另一个网站。然后在指向 abc.com 的父目录中创建一个 .htaccess

RewriteEngine on

RewriteCond %{HTTP_HOST} ^abc.com$ [NC,OR]

RewriteCond %{HTTP_HOST} ^www.abc.com$ 

RewriteCond %{REQUEST_URI} !subdirectory/

RewriteRule (.*) /subdirectory/ [L]

回答by Felipe Alameda A

Rewriting all image requests to root could be a solution. You may try this in one .htaccess file at root directory:

将所有图像请求重写为 root 可能是一个解决方案。您可以在根目录下的一个 .htaccess 文件中尝试此操作:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Check if request is for an image at root. Add/remove file types if necessary.
RewriteCond %{REQUEST_URI}  ^/([^.]+)\.(png|jpg|gif|css) [NC]
RewriteCond %{REQUEST_URI}  !/website_1  [NC]
# Rewrite request to point to "website_1" directory
RewriteRule .*               /website_1/%1.%2          [L,NC]