简体   繁体   中英

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

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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