简体   繁体   English

Apache .htaccess RewriteRule

[英]Apache .htaccess RewriteRule

Here's my situation. 这是我的情况。 I have a web root and several subdirectories, let's say: 我有一个网络根目录和几个子目录,可以这样说:

/var/www
/var/www/site1
/var/www/site2

Due to certain limitations, I need the ability to keep one single domain and have separate folders like this. 由于某些限制,我需要能够保留一个域并拥有这样的单独文件夹。 This will work fine for me, but many JS and CSS references in both sites point to things like: 这对我来说很好用,但是两个站点中的许多JS和CSS引用都指向如下内容:

"/js/file.js"
"/css/file.css"

Because these files are referenced absolutely, they are looking for the 'js' and 'css' directories in /var/www, which of course does not exist. 因为这些文件是绝对引用的,所以它们正在/ var / www中查找“ js”和“ css”目录,这些目录当然不存在。 Is there a way to use RewriteRules to redirect requests for absolutely referenced files to point to the correct subdirectory? 有没有一种方法可以使用RewriteRules重定向对绝对引用文件的请求,使其指向正确的子目录? I have tried doing things like: 我曾尝试做类似的事情:

RewriteEngine on
RewriteRule ^/$ /site1

or 要么

RewriteEngine on
RewriteRule ^/js/(.*)$ /site1/js/$1
RewriteRule ^/css/(.*)$ /site1/css/$1

But neither of these work, even redirecting to only one directory, not to mention handling both site1 and site2. 但是这些都不起作用,甚至重定向到一个目录也没有,更不用说同时处理site1和site2了。 Is what I'm trying possible? 我正在尝试的可能吗?

EDIT: SOLUTION 编辑:解决方案

I ended up adapting Jon's advice to fit my situation. 我最终适应了乔恩的建议以适应我的情况。 I have the ability to programatically make changes to my .htaccess file whenever a new subdirectory is added or removed. 每当添加或删除新的子目录时,我都可以以编程方式对.htaccess文件进行更改。 For each "site" that I want, I have the following section in my .htaccess: 对于我想要的每个“站点”,我的.htaccess中都有以下部分:

RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{HTTP_COOKIE} sitename=site1
RewriteCond %{REQUEST_URI} !^/site1/
RewriteRule ^(.*)$ /site1/$1 [L]

Index.php is a file that lists all my sites, deletes the "sitename" cookie, and sets a cookie of "sitename=site#" when a particular one is selected. Index.php是一个文件,其中列出了我的所有站点,删除了“ sitename” cookie,并在选择了特定站点时将cookie设置为“ sitename = site#”。 My RewriteConds check, 我的RewriteConds检查

  1. If the request is not for / 如果请求不是针对/
  2. If the request is not for /index.php 如果请求不是针对/index.php的
  3. If the request contains the cookie "sitename=site1" 如果请求包含cookie“ sitename = site1”
  4. If the request does not start with "/site1/" 如果请求不是以“ / site1 /”开头

If all of these conditions are met, then the request is rewritten to prepend "/site1/" before the request. 如果满足所有这些条件,则将请求重写为在请求之前添加“ / site1 /”。 I tried having a single set of Conds/Rules that would match (\\w+) instead of "site1" in the third Condition, and then refer to %1 in the fourth Condition and in the Rule, but this did not work. 我尝试在第三个条件中使用一组匹配(\\ w +)而不是“ site1”的条件/规则,然后在第四个条件和规则中引用%1,但这不起作用。 I gave up and settled for this. 我放弃了,为此解决了。

If the RewriteRules are in your .htaccess file, you need to remove the leading slashes in your match (apache strips them before sending it to mod_rewrite). 如果RewriteRules位于您的.htaccess文件中,则需要删除匹配中的前导斜杠(在将其发送到mod_rewrite之前,先将其删除)。 Does this work? 这样行吗?

RewriteEngine on
RewriteRule ^js/(.*)$ /site1/js/$1
RewriteRule ^css/(.*)$ /site1/css/$1

EDIT: To address the comment: 编辑:解决评论:

Yes, that works, but when I do RewriteRule ^(.*)$ /site1/$1 , it causes Apache to issue internal server errors. 是的,可以,但是当我执行RewriteRule ^(.*)$ /site1/$1 ,它将导致Apache发出内部服务器错误。 But to me, it seems like that should just be a generic equivalent of the individual rules! 但是在我看来,这应该只是各个规则的通用等效项!

What's happening with that rule is when /something/ gets rewritten to /site/something/ , and apache internally redirects, it gets rewritten again , to /site/site/something/ , then again, then again, etc. 该规则发生的事情是当/something/被重写为/site/something/ ,并且apache在内部进行重定向时,它再次被重写为/site/site/something/ ,然后再次被重写,等等。

You'd need to add a condition to that, something like: 您需要为此添加一个条件,例如:

RewriteCond %{REQUEST_URI} !^/site/
RewirteRule ^(.*)$ /site/$1 [L]

您需要设置符号链接 ,重写规则将使用它们,以便服务器级别的绝对链接可以跟随到中央站点托管帐户的符号链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM