Mod_rewrite in a subdirectory

Our web server is structured as follows

/path-to-docdir [Contains main pages] ./app/ [Some application not directly related to the main content] 

Main pages are excellent. In the application directory, I am trying to use mod_rewrite to redirect all pages to index.php in the public directory

So the request

 /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 the controller and action.

To do this, I 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 found that the server goes into a redirect loop for the above URLs. I tried replacing $1 with index.php and this calls the page, but internal links inside the page to the script lead to errors.

After some searching, I added the following line:

 RewriteBase /app/ 

And that didn't seem to make any difference. I should also note that everything works as expected if I create the application directory as the server root directory.

I also turned on debug logging, and all I could collect was that the server really went into the redirect loop.

  [perdir / path-to-docdir / app /] internal redirect with / app / public / public / public / public / public / public / public / public / public / public / start / login / [INTERNAL REDIRECT]

I'm not quite sure where to start looking at this point, so any help or pointers to move me further will be greatly appreciated.

Edit:

This htaccess file worked for me - if someone else is looking for a solution. Apparently I didn’t have enough rewriting conditions

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)$ public/index.php?$1 [L] </IfModule> 
+4
source share
1 answer

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/ redundant. The first will correspond to http://yourserver/app and internally rewrite as http://yourserver/app/public , so there is no need for the latter.

Finally, try adding a rewrite condition:

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

All Articles