mod_rewrite in a subdirectory

apache, mod-rewrite

Solution

A few points:

A `RewriteBase` path does not need a trailing slash, so `RewriteBase /app/` should be `RewriteBase /app`.

`RewriteRule ^(.*)$ public/$1` makes `RewriteRule ^$ public/` superfluous. The former will match `http://yourserver/app` and internally rewrite as `http://yourserver/app/public`, so there's no need for the latter.

Finally, try adding a rewrite condition:

RewriteEngine On
RewriteBase /app
RewriteCond %{REQUEST_URI} !^/app/public/
RewriteRule ^(.*)$ public/$1 [L]

Problem

Our web server is structured as follows ``` /path-to-docdir [Contains main pages] ./app/ [Some application not directly related to the main content] ``` The main pages are served fine. In the app directory I am trying to use mod_rewrite to redirect all pages to an `index.php` in a public directory So a request to ``` /server/app/start/login/ => /server/app/public/index.php /server/app/start/login => /server/app/public/index.php /server/app/start/login?name=someone => /server/app/public/index.php?name=someone ``` The front controller relies on the last two components of the `REQUEST_URI` string to interpret controller and action. To do the above I have created the following `.htaccess` file in the `app` directory ``` <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^$ public/ [L] RewriteRule ^(.*)$ public/$1 [L] </IfModule> ``` Here, I find that the server goes in a redirect loop for the above URLs. I tried replacing `$1` with `index.php` and that does bring up the page, but the internal references within the page to script files run into errors. After some searching around, I added the following line: ``` RewriteBase /app/ ``` And that did not seem to make any difference. I should also note that everything works as expected if I make the app directory as the server root directory. I've also turned on debug logging and all I could gather from that was that the server was indeed going into a redirect loop. ``` [perdir /path-to-docdir/app/] internal redirect with /app/public/public/public/public/public/public/public/public/public/public/public/start/login/ [INTERNAL REDIRECT] ``` I'm not really sure where to start looking at this point, so any help or pointers to move me further would be much appreciated. Edit: This htaccess file worked for me - if anyone else is looking for a solution. Apparently, I was missing rewrite conditions ``` <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)$ public/index.php?$1 [L] </IfModule> ```

Original source