Tony
Tony

Reputation:

mod_rewrite error 404 if .php

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.

Upvotes: 8

Views: 4454

Answers (3)

Alex Barrett
Alex Barrett

Reputation: 16475

A 301 redirect may be more appropriate for this task.

Upvotes: 2

cletus
cletus

Reputation: 625317

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

Upvotes: 7

Gabriel Hurley
Gabriel Hurley

Reputation: 40072

This should do the trick, I think:

RewriteRule (.*)\.php$ path/to/your/404file [L]

Upvotes: 2

Related Questions