Benjie Wheeler
Benjie Wheeler

Reputation: 575

How to force hide .php extension with .htaccess

Hi everyone this is my .htaccess file code

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

It works perfectly but can I force hide the .php extension, so even if I went to www.example.com/foo.php I get redirected to www.example.com/foo

Is there any way I can do that?

Upvotes: 1

Views: 632

Answers (1)

Jon Lin
Jon Lin

Reputation: 143966

Add this to your htaccess file:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]

This matches against a request for a php file, and then redirects the browser to the same request but without the .php extension.

Upvotes: 2

Related Questions