简体   繁体   English

.htaccess RewriteRule导致“找不到文件”

[英].htaccess RewriteRule causing “file not found”

My website is hosted in a subfolder : site.com/foo/ 我的网站托管在一个子文件夹: site.com/foo/

In the root of the foo/ folder I have the following .htaccess : foo/文件夹的根目录中,我有以下.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule (.*) webroot/$1 [L]
</IfModule>

It's role is to redirect all page requests to a webroot/ subfolder located within the foo/ folder. 它的作用是将所有页面请求重定向到位于foo/文件夹中的webroot/子文件夹。 In that webroot/ subfolder, I have the following .htaccess : 在该webroot/子文件夹中,我具有以下.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]

The goal is that all should be redirected to that index.php . 目标是将所有内容都重定向到该index.php To be clear, file structure is : 要明确的是,文件结构为:

www/
  foo/
      .htaccess
      webroot/
         .htaccess
         index.php

When I try accessing site.com/foo/ , I get index.php as expected, but when I try random things such as : site.com/foo/bar/bar/bar , I get "file not found"(that's all) but I sill wanted index.php . 当我尝试访问site.com/foo/ ,按预期方式获得了index.php ,但是当我尝试诸如以下内容的随机事件时: site.com/foo/bar/bar/bar ,我得到了“找不到文件”(仅此而已) ),但我仍然想要index.php

The reason it works only when accessing site.com/foo/ has nothing to do with the second .htaccess , it's just webroot/ serving its contained index.php by default. 它的工作原理,只有当访问的原因site.com/foo/无关与第二.htaccess ,它只是webroot/服务的默认包含的index.php。

Notice : it worked in localhost , and the main difference between localhost and the server I'm using now is probably that the site was in ROOT for localhost but within a subfolder now ( foo/ ). 注意:它的工作在localhost ,之间的主要区别localhost ,我现在使用的服务器可能是该网站在ROOTlocalhost ,但子文件夹现在(中foo/ )。

Notice 2 : If I remove /$1 from index.php/$1 , it works, it redirect ALL to index.php, but without the parameter which I need ( bar/bar/bar in the above example.) 注意2:如果我从index.php/$1删除/$1 ,它可以工作,它将ALL重定向到index.php,但是没有我需要的参数(在上面的示例中为bar/bar/bar )。

How can I get all request to be redirected to that index.php with parameters? 如何获取所有带参数的请求重定向到该index.php?

Try adding RewriteBase in both .htaccess: 尝试在两个.htaccess中添加RewriteBase

/foo/.htaccess: /foo/.htaccess:

RewriteEngine On
RewriteBase /foo/

RewriteRule (.*) webroot/$1 [L]

/foo/webroot/.htaccess: /foo/webroot/.htaccess:

RewriteEngine On
RewriteBase /foo/webroot/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]

QSA flag appends the query string- QSA标志附加查询字符串-

RewriteRule (.*) index.php?data=$1 [QSA] RewriteRule(。*)index.php?data = $ 1 [QSA]

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

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