Apache .htaccess 重写规则

发布于 2024-12-14 08:39:51 字数 1305 浏览 0 评论 0原文

这是我的情况。我有一个网络根目录和几个子目录,比方说:

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

由于某些限制,我需要能够保留一个域并拥有像这样的单独文件夹。这对我来说效果很好,但是两个站点中的许多 JS 和 CSS 引用都指向这样的内容:

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

因为这些文件是绝对引用的,所以它们正在 /var/www 中寻找 'js' 和 'css' 目录,这当然是不存在。有没有办法使用 RewriteRules 重定向对绝对引用文件的请求以指向正确的子目录?我尝试过做类似的事情:

RewriteEngine on
RewriteRule ^/$ /site1

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

但是这些都不起作用,甚至只重定向到一个目录,更不用说处理 site1 和 site2 了。我正在尝试的可能吗?

编辑:解决方案

我最终调整了乔恩的建议以适应我的情况。每当添加或删除新的子目录时,我都能够以编程方式更改我的 .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 是一个列出我的所有站点的文件,删除“sitename”cookie,并在以下情况下设置“sitename=site#”的 cookie:选择一个特定的。我的 RewriteConds 检查,

  1. 如果请求不是 /
  2. 如果请求不是 /index.php
  3. 如果请求包含 cookie“sitename=site1”
  4. 如果请求不是以“/site1/”开头

如果所有这些条件都满足满足,则请求被重写以在请求之前添加“/site1/”。我尝试使用一组条件/规则来匹配第三个条件中的 (\w+) 而不是“site1”,然后在第四个条件和规则中引用 %1,但这不起作用。我放弃了并接受了这一点。

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/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. Is there a way to use RewriteRules to redirect requests for absolutely referenced files to point to the correct subdirectory? 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. 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. For each "site" that I want, I have the following section in my .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. My RewriteConds check,

  1. If the request is not for /
  2. If the request is not for /index.php
  3. If the request contains the cookie "sitename=site1"
  4. If the request does not start with "/site1/"

If all of these conditions are met, then the request is rewritten to prepend "/site1/" before the request. 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. I gave up and settled for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-12-21 08:39:52

如果 RewriteRules 位于 .htaccess 文件中,则需要删除匹配项中的前导斜杠(apache 在将其发送到 mod_rewrite 之前将其删除)。这有效吗?

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

编辑:要解决评论:

是的,这可行,但是当我执行 RewriteRule ^(.*)$ /site1/$1 时,它会导致 Apache 发出内部服务器错误。但对我来说,这似乎应该只是个别规则的通用等效项!

该规则发生的情况是,当 /something/ 被重写为 /site/something/ 时,并且 apache 内部重定向,它会再次被重写,到 /site/site/something/,然后再一次,然后再一次,等等。

您需要为此添加一个条件,例如:

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

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

You'd need to add a condition to that, something like:

RewriteCond %{REQUEST_URI} !^/site/
RewirteRule ^(.*)$ /site/$1 [L]
陪你搞怪i 2024-12-21 08:39:52

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

You need to set up symlinks, which the rewrite rules will use so your absolute links at the server level can follow the symbolic links to the central site hosting account.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文