简体   繁体   English

.htaccess-将其他内容重写为index.php

[英].htaccess - rewrite anything else to index.php

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. 我希望将以(css | im | js | lib)/开头的路径重定向到公用文件夹,以及其他所有重定向到index.php文件。

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 例如, http: //mysite.com/css/s.css应该重定向到SITE_ROOT / public / css / s.css,而http://mysite.com/absde应该重定向到SITE_ROOT / index.php

Here's working config for nginx: 这是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: 现在,我尝试为apache重写此代码,在.htaccess中写类似的代码:

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

This does not work. 这是行不通的。 It seems RewriteRule . 看来RewriteRule。 index.php applies before the second line so that everything goes to index.php. index.php在第二行之前适用,以便所有内容都进入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. 现在, http ://mysite.com/abcde/css/s.css重写为css / s.css,但事实并非如此。 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. 请帮忙。

The problem is that internal redirects are treated as new requests, and so the rules are being evaluated again, where the first rule now won't match anymore, but the second one does, and so you'll always end up at index.php . 问题在于内部重定向被视为新请求,因此再次评估了规则,现在第一个规则不再匹配,但是第二个规则匹配了,因此您总是以index.php结尾。

One way to avoid this would be to bind the last rule to the condition that it doesn't start with /public/ : 避免这种情况的一种方法是将最后一个规则绑定到不以/public/开头的条件:

RewriteCond %{REQUEST_URI} !^/public/.*
RewriteRule . index.php [L]

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

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