4

My htaccess doesn't quite work:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]
RewriteRule ^(.*)$ /blah/index.php/$1 [L]

Basically /blah/about.php should redirect to /blah/about

I'm using codeigniter so the last line is required to process URL's.

The redirect works fine but I get an internal server error in /blah/about

** Edit - error log:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

fire
  • 21,383
  • 17
  • 79
  • 114
  • What do the server error logs say? Why are you using a 301 to redirect? – Pekka Feb 15 '10 at 16:51
  • Because I want to change the site structure in an SEO friendly way i.e. using 301 redirects – fire Feb 15 '10 at 16:53
  • Then I guess the problem is not in the redirect if you get an internal server error but in the file and has nothing to do with redirection. – Felix Kling Feb 15 '10 at 16:58
  • Does the 500 error go away if you comment out your first RewriteRule? – nortron Feb 15 '10 at 17:05
  • You mean the `%{REQUEST_FILENAME} !-f` ? Nope :( – fire Feb 15 '10 at 17:07
  • No, remove the first RewriteRule just after your RewriteConds and see if it removes the problem. If it does check my answer for a possible fix, I just updated it. – nortron Feb 15 '10 at 17:08
  • Specifically, remove 'RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]' just for test purposes to see if that's the root of the problem. – nortron Feb 15 '10 at 17:10
  • Yes that's what I have established, removing the redirect rule fixes the interal error but I need the redirect in there (hence the reason for this post). I could just extend the helper functions but I was hoping for a quick fix... – fire Feb 15 '10 at 17:11
  • 2
    Just wanted to make sure. Try duplicating the RewriteCond lines for your second Rule like I have in my answer below, does that change anything? – nortron Feb 15 '10 at 17:13

4 Answers4

3

I think you just need to change your last line to be:

RewriteRule ^(.*)$ /blah/index.php?/$1 [L]

I have no specific experience with CodeIgniter but generally frameworks want to push all requests through a central controller (index.php) and then parse the request's URL from within there.

EDIT: Just updated my answer with the example from here: http://codeigniter.com/wiki/mod_rewrite/, that should be how CodeIgniter wants it.

UPDATE: You should probably duplicate your RewriteConds for the second RewriteRule, they only apply to the first:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blah/index.php/$1 [L]
nortron
  • 3,873
  • 1
  • 24
  • 27
  • Nope CI definetly needs that part :( – fire Feb 15 '10 at 16:56
  • @fire just added ?/$1 to that URL per the link I posted, that should do it. – nortron Feb 15 '10 at 16:58
  • I keep getting: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. – fire Feb 15 '10 at 16:59
2

Thanks @Cryo for his help, this is the fix:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /blah/index.php/$1 [L]
fire
  • 21,383
  • 17
  • 79
  • 114
1

What about:

RewriteRule ^(.*)(\.php)?$ /blah/index.php/$1 [L]

and remove the previous line.

Also make sure that /blah/about.php does not exist.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
-1
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [NC]
s3yfullah
  • 165
  • 1
  • 6
  • 22