【问题标题】:Redirect http to https and remove .html from pages将 http 重定向到 https 并从页面中删除 .html
【发布时间】:2017-09-27 21:19:56
【问题描述】:

说实话,我很不熟悉.htaccess,但我想要完成的是:

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

  2. 所有页面都从 URL 中删除 .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 重定向您的次数过多。

有什么想法吗?

【问题讨论】:

  • 请举例说明您请求的网址。是否所有 URL 都通过简单地附加 .html 直接映射到 .html 文件?尝试禁用MultiViews。 IE。 Options -MultiViews 在文件顶部。

标签: .htaccess


【解决方案1】:

尝试以下规则:

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

#the rules 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 rules 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 .  

【讨论】:

    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 2016-09-16
    相关资源
    最近更新 更多