Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

.htaccess - remove .html extension from url

I'm trying to remove the .html extension from url for my website by editing the .htaccess file under the web root(public_html) directory. My server is bluehost.

The problem is that I have previous settings in the .htaccess file already for the redirecting primary domain to a subdirectory purpose.

The script is below. Also with the script i found online for hiding the extension from url.(The script doesn't work) It will be really nice if someone can help me out with the problem. Thanks.

# Use PHP5.4 Single php.ini as default
AddHandler application/x-httpd-php54s .php


# BlueHost.com 
# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?qinglish.ca$ 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/qinglish_ca/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$ /qinglish_ca/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?qinglish.ca$ 
RewriteRule ^(/)?$ qinglish_ca/index.html [L]

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of your last rule you will need these rules to remove .html extensions fro all URLs:

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

# To internally forward /dir/file to /dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]

Place them at same place where you have current rule to add .html extension.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...