0

I have app and public folders in my project. I want paths that starts with (css|im|js|lib)/ to be redirected to public folder, and anything else - to index.php file.

For example, http://mysite.com/css/s.css should be redirected to SITE_ROOT/public/css/s.css and http://mysite.com/absde should be redirected to SITE_ROOT/index.php

Here's working config for nginx:

server {
  listen 80;
  server_name mysite.com;
  root /home/mysite/html;

  location ~ \/(css|im|js|lib)\/ {
    root /home/mysite/html/public;
    expires 30;
    try_files $uri =404;
  }

  location {
    rewrite ^(.*)$ /index.php break;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

Now I try to rewrite this for apache, I write something like that in .htaccess:

RewriteEngine On
RewriteRule ^(css|im|js|lib)\/(.*)$ public/$1/$2 [L]
RewriteRule . index.php [L]

This does not work. It seems RewriteRule . index.php applies before the second line so that everything goes to index.php. But why?

Nothing changes if I change the third row to

RewriteRule ^(.*)$ index.php [L]

And that means that the type of equation doesn't affect on the order of rules.

And only if I remove ^ symbol from the regex in the second line, site starts working as expected. But I don't want to remove it! Now http://mysite.com/abcde/css/s.css rewrites to css/s.css, and it shouldn't be that way. Also the second line starts working if I remove the third one (that means it's correct by itself).

That's so stupid simple but I can't find a mistake in theese three rows. Please help.

4

1 回答 1

1

问题是内部重定向被视为新请求,因此再次评估规则,第一个规则现在不再匹配,但第二个匹配,所以你总是会在index.php.

避免这种情况的一种方法是将最后一条规则绑定到它不以开头的条件/public/

RewriteCond %{REQUEST_URI} !^/public/.*
RewriteRule . index.php [L]
于 2013-08-14T14:38:58.653 回答