简体   繁体   中英

htaccess prevent page extensions displaying

I have my htaccess file set up so that my website pages don't need to display an extension, my links are setup like this, with the slash at the end:

 www.mywebsite/about/

These links work - But if I were to directly type in

 www.mywebsite/about.html 

The website will show this page with the html extension. Is there a way that I can prevent extensions from showing even if they are directly typed in? :S

The majority of my pages are html, except for one php page. This is what I have in my htaccess:

Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]

If anyone can help me out with this I would really appreciate it! :)

Please try this:

Options -Indexes
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule .* %1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]

RewriteRule ^php-page/([^/\.]+)/?$ php-page.php?p=$1 [L]

I think it can be done a bit better than this, but it should work.

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