将http重定向到https并从页面中删除.html

时间:2017-09-27 21:19:56

标签: .htaccess

我非常不熟悉.htaccess如何诚实,但我想要完成的是:

  1. 所有http请求都重定向到https

  2. 所有网页都会从网址

  3. 中删除.html

    我有这段代码:

    RewriteEngine On 
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    
    #example.com/page will display the contents of example.com/page.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+)$ $1.html [L,QSA]
    
    #301 from example.com/page.html to example.com/page
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
    RewriteRule ^(.*)\.html$ /$1 [R=301,L]
    

    但我在Chrome中遇到错误:

      

    example.com重定向了你太多次了。

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://yoursite.com/$1 [L,R]

#the code above will redirect the entire site into https without www unless the request come with https://www .      

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

#the code above will remove any .HTML.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

 # you can use the two lines if file without extension not work and you want to map them to .html files and if you want to go the same file .html without extension keep it as it is .