简体   繁体   中英

.htaccess RewriteRule causing “file not found”

My website is hosted in a subfolder : site.com/foo/

In the root of the foo/ folder I have the following .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. In that webroot/ subfolder, I have the following .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 . 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 .

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.

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

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

How can I get all request to be redirected to that index.php with parameters?

Try adding RewriteBase in both .htaccess:

/foo/.htaccess:

RewriteEngine On
RewriteBase /foo/

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

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

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

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