简体   繁体   中英

htaccess rewrite rule extension with slash error

I have removed my url extension(.php) using htaccess, even made url with extension .php redirect to 404 error, like if my url is www.example.com/page/ and some one adds .php extension at the end like www.example.com/page.php the page redirects as it should but the problem is when I open the url: "www.example.com/page.php/" with the extension and the slash it opens. No redirection, nothing. I want that to redirect to 404.

Kindly review my htaccess contents and suggest me improvements, if needed. Any help would be greatly appreciated.

non-www to www

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^example.com [nocase]
RewriteRule ^(.*)         http://www.example.com/$1 [last,redirect=301]

index page to only domain

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index/
RewriteRule ^index/$ http://www.example.com/ [R=301,L]

removes extension and adds slash

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

removes multiple slashes

RewriteBase /
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=404,L]

You need a new rule just below www rule to remove .php extensions:

RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

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