1

I'm trying to create my mod_rewrite file so that basically all of the .php extensions are removed when you view a file.

Here's my .htaccess:

RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 /errors/404.php
ErrorDocument 403 /errors/403.php
ErrorDocument 500 /errors/500.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

Now here's the error i get from my apache log when trying a random name:

[Tue Jan 31 17:26:05 2012] [error] [client 127.0.0.1] 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.
[Tue Jan 31 17:26:05 2012] [debug] core.c(3112): [client 127.0.0.1] r->uri = /aboutasdfoi.php.php.php.php.php.php.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php.php.php.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php.php.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi.php
[Tue Jan 31 17:26:05 2012] [debug] core.c(3118): [client 127.0.0.1] redirected from r->uri = /aboutasdfoi

How do i go about fixing it? Thanks

8
  • 1
    Remove the recursion, then you won't hit the limit. Commented Jan 31, 2012 at 17:34
  • What are you trying to redirect to? Commented Jan 31, 2012 at 17:39
  • Where is the recursion? I'm pretty bad at .htaccess writing so this was generated and partly written! Commented Jan 31, 2012 at 17:40
  • Well i simply want to remove the .php to all files i have so for example about.php, index.php etc. And then I obviously have the ErrorDocuments relating to each error! The test for "aboutasdfoi" was just a test as aboutasdfoi.php doesn't exist, so I was hoping it would go to my 404 page! Commented Jan 31, 2012 at 17:41
  • EDIT: NOT SOLVED! Ok I solved the error myself, just found out it's the ( and ) causing it, which i assume is the recursion :) I'll look into it more now! Commented Jan 31, 2012 at 17:43

1 Answer 1

2

To hide .php extension use this code:

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
Sign up to request clarification or add additional context in comments.

4 Comments

You're welcome. If it works for you then can you accept this answer to close this Q&A.
anubhava, have you tried a test vector fred?param=1. Your algo will generate fred?param=1.php.php.php.php etc. before barfing on a recursion limit. %{REQUEST_URI} includes any get parameters.
@TerryE wrote: %{REQUEST_URI} includes any get parameters. Sorry for being blunt but you are absolutely wrong. Pls read some Apache docs on mod_rewrite before making comments like this. Just fyi: %{REQUEST_URI} contains only the URI and %{QUERY_STRING} actually has all the Get parameters.
:Oops: Got to +1 you on that. My confusion is that the Environment variable REQUEST_URI does, but the rewrite variable %{REQUEST_URI} doesn't as you can see here. This is just a phpinfo() but I've added a diagnostic RewriteRule .? - [E=REQ_URI:%{REQUEST_URI}] to my docroot .htaccess file. Search for "REQUEST_URI" and "REDIRECT_REQ_URI".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.