Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let me preface by saying that I'm fairly new to .htaccess authoring and have usually left the nitty gritty up to my hosting provider. I've recently decided to implement a hand rolled MVC framework in php and was using the .htaccess file to redirect using "seo" friendly urls. My MVC implementation uses the variables module, class, event, and parameter. They are specified in the url in that order, e.g. I want it to work if you chop off any part of the url. This was all working fine until I moved my site up one level and subsequently copied my .htaccess file. Now I'm getting an infinite redirect loop with one of the rules that was working fine before I moved it.

Here is my .htaccess file:

Options +FollowSymLinks 
RewriteEngine On 

RewriteRule ^$ /index.php?module=default&class=home [QSA,L]

RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ \
    /index.php?module=$1&class=$2&event=$3 [QSA,L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ \
    /index.php?module=$1&class=$2&event=$3&parameter=$4 [QSA,L]

Now, if I take out the section that reads:

RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]

It works great. The problem is if I leave that out and someone types I get a 404 (since that directory doesn't exist and none of the rules are matching). So why does that rule now not work and why did it work when index.php (and this .htaccess file) are in a sub-directory?

share|improve this question

marked as duplicate by Jenny D, Ward, Michael Hampton Feb 28 '14 at 21:20

This question was marked as an exact duplicate of an existing question.

As I understand the problem is solved?… – LazyOne Jun 23 '11 at 18:00
@LazyOne Yes, I'll post my solution here as well. Thanks! – SRM Jun 23 '11 at 18:41
This question appears to be off-topic because it is asked and answered on… – Jenny D Feb 26 '14 at 9:25

Well it looks like the php router solution is the best.

Here is the simplified .htaccess I am now using

RewriteEngine On

# Disable rewriting for existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# redirect all other requests to index.php
RewriteRule ^.*$ index.php [PT,L]
RewriteRule ^$ index.php [PT,L]

And this "magic" line of code does all the heavy lifting that the .htaccess was doing before:

$url = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

Now I can assign my module, class, event, and parameter values from the $url array. It also solves a problem I had where I may need more than one parameter to an event. I didn't want to have to keep adding rules to the .htaccess file for each level. Now I can supply an arbitrary number of parameters and deal with the routing logic in code.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.