0

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. http://mydomain.com/module/class/event/parameter. 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 

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^$ /index.php?module=default&class=home [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ \
    /index.php?module=$1&class=$2&event=$3 [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ \
    /index.php?module=$1&class=$2&event=$3&parameter=$4 [QSA,L]

Now, if I take out the section that reads:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]

It works great. The problem is if I leave that out and someone types http://mydomain.com/module 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?

SRM
  • 109
  • 4
  • As I understand the problem is solved? http://stackoverflow.com/questions/6457430/help-with-infinite-redirect-from-htaccess-using-mvc-with-php – 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 http://stackoverflow.com/questions/6457430/help-with-infinite-redirect-from-htaccess-using-mvc-with-php – Jenny D Feb 26 '14 at 09:25

1 Answers1

0

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.

SRM
  • 109
  • 4