1

I want to hide .php extension so I wrote following code in .htaccess file which I found at this link How to remove file extension from website address?

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

This work fine when I type localhost/testsite/index it displays correctly localhost/testsite/index.php and display localhost/testsite/index in address bar as I want, but when I forcefully type localhost/testsite/index.php it does not converted to localhost/testsite/index. I want to remove extension even when user type .php after page name.

Community
  • 1
  • 1
Sumit P Makwana
  • 317
  • 5
  • 17
  • I am afraid you cant. I have more than average experience with htaccess files and it works the way you wrote. You can still access the full path instead of the rewriten if you write it manually and i dont see anything wrong in there! :) – Cowwando Sep 03 '14 at 05:09
  • Is this a duplicate of http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – jbrahy Sep 03 '14 at 05:11
  • in stackoverflow site for example if i write stackoverflow.com/questions/25636602/automatic-url-rewriting-not-working.php?noredirect=1#comment40055522_25636602 it will display stackoverflow.com/questions/25636602/automatic-url-rewriting-not-working?noredirect=1#comment40055522_25636602 and will remove .php extension. – Sumit P Makwana Sep 03 '14 at 05:18
  • No it is not a duplicate of http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – Sumit P Makwana Sep 03 '14 at 05:21

1 Answers1

0

Have your .htaccess like this:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643