如何设置文档根使用的.htaccess而不是虚拟主机是一个子目录 [英] How to set document root to be a subdirectory using .htaccess and not VHost

查看:240
本文介绍了如何设置文档根使用的.htaccess而不是虚拟主机是一个子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的本地机器下面可以完美运行:

On my local machine the following works perfect:

 <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>

Howerver林托管我的网站托管公司名为 justhost.com ,然后他们不允许我修改的httpd-vhosts.conf 的httpd.conf 。现在,我的整个网站为codeD,以便在使用WEBSITE_1下WEBSITE_1参考其他文件的文件简单的斜杠/,意思WEBSITE_1被视为文档根目录。这工作完全在本地计算机上,但是当上传来承载它给我的服务器错误,因为无法找到该文件,因为它试图找到这些文件中的的public_html

Howerver Im hosting my website to hosting company called justhost.com and they do not allow me to modify httpd-vhosts.conf or 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

例如:

  public_html
      - website_1
         - script.php
         - style.css

在我的的script.php

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

这个工程很好的本地计算机上,但在主机上失败,因为它试图在寻找的style.css 的public_html 而非的public_html / WEBSITE_1

有没有办法有多个文档根目录,而无需使用虚拟主机?喜欢使用的.htaccess或别的东西。请我想尽量避免重写code尽可能因为code的约10千行。

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.

推荐答案

的httpd.conf 启用了mod_rewrite和.htaccess,然后把这个code在< $ C C>的.htaccess $ DOCUMENT_ROOT 目录下:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

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 。避免使用 R = 301 (永久重定向),而测试你的mod_rewrite规则。

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

这篇关于如何设置文档根使用的.htaccess而不是虚拟主机是一个子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆