8

I rewrite my urls to be user friendly. For example I have a page called user.php that I rewrite to /user. But a user can still use user.php. Can I redirect to a 404 if they request a page with a .php extension?

Options -MultiViews +FollowSymlinks -Indexes
RewriteEngine on


RewriteRule ^user/([0-9]+)$ user.php?id=$1 [L,QSA]

Thanks.

3 Answers3

7
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
cletus
  • 616,129
  • 168
  • 910
  • 942
  • that works but i can still access the page if i add a question mark after user.php?. maybe i can do something about that to? –  Jun 28 '09 at 20:30
  • R=404 is allowed, however apache only sends the `Location` header is the http-code is in the 300 range. – Gerben Jan 11 '12 at 15:47
  • you should add [nc], otherwise pages will load with uppercase extensions – M_R_K Jun 23 '15 at 09:03
2

This should do the trick, I think:

RewriteRule (.*)\.php$ path/to/your/404file [L]
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
2

A 301 redirect may be more appropriate for this task.

Alex Barrett
  • 16,175
  • 3
  • 52
  • 51