简体   繁体   中英

Automatic URL Rewriting not working

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM