【问题标题】:htaccess - HTTP to HTTPS and .HTML to /htaccess - HTTP 到 HTTPS 和 .HTML 到 /
【发布时间】:2016-03-10 22:01:27
【问题描述】:

我需要.htaccess 的帮助:

强制 http://https://

强制 .html/

到目前为止我所拥有的:

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

【问题讨论】:

    标签: .htaccess http https


    【解决方案1】:
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]
    

    我以前用过这两个,但我没有一起用过。希望这会有所帮助。

    (编辑) 要实际强制扩展看起来像目录:

    RewriteBase /
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
    RewriteRule ^ /%1/ [R=302,L,NE]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
    RewriteRule ^(.+?)/?$ $1.html [L]
    

    http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file

    【讨论】:

    • 这样更好,不幸的是 /example.html 没有强制 /example
    • 通过强制您将其 URL 从 /anything.html 更改为 /anything 或只是任何 / 或 any.html 都可以吗?
    【解决方案2】:
    Options -MultiViews
    
    RewriteEngine On
    #1 This line checks if the https is off
    RewriteCond %{HTTPS} ^off$
    #then, redirect to https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
    #2 this line checks if the request is /file.html
    RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
    #then redirect /file.html to /file
    RewriteRule ^ /%1 [NC,L,R]
    
    #3 if the request is not for dir
    RewriteCond %{REQUEST_FILENAME} !-d
    #and the request is an existing filename
    RewriteCond %{REQUEST_FILENAME}.html -f
    #then rewrite /file to /file.html
    RewriteRule ^([^/]+)/?$ $1.html [NC,L]
    

    在上面的例子中,当原始方案是HTTP时满足第一个条件,然后处理规则。 HTTP 转到 HTTPS。第一轮重写处理到此结束。

    在第二轮中,mod_rewrite 接受URI /file.html 并且规则将其重定向到/file,因为/file 不存在于目录中,所以我们需要将其重写为原始文件#3...

    【讨论】:

      【解决方案3】:

      首先,确保mod-rewrite 已启用。
      然后,将此代码放入您的 htaccess 中(应该在文档根文件夹中)

      Options -MultiViews
      
      RewriteEngine On
      RewriteBase /
      
      # Redirect http urls to https equivalent
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R=301,L]
      
      # Redirect existing /path/file.html to /path/file/
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteCond %{THE_REQUEST} \s/(.+?)\.html\s [NC]
      RewriteRule ^ %1/ [R=301,L]
      
      # Internally rewrite back /path/file/ to /path/file.html (if existing)
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
      RewriteRule ^(.+)/$ $1.html [L]
      

      警告

      1. 确保为 https 提供与 http 相同的文档根目录(apache ssl 块配置)
      2. 请注意,这会创建虚拟目录(通过添加尾随 斜线),这可能会弄乱您的 html 资源(如果您正在使用 相对路径而不是绝对路径)。如果是这样,请使用绝对路径 而是

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-21
        • 1970-01-01
        • 2013-04-22
        • 2017-12-28
        • 2021-03-25
        • 2012-05-16
        • 1970-01-01
        • 2016-01-20
        相关资源
        最近更新 更多