【问题标题】:htacess: triple rewrite HTTPS + no-WWW + no-.htmlhtaccess:三重重写 HTTPS + 非 WWW + no-.html
【发布时间】:2014-05-02 08:02:13
【问题描述】:

我正在尝试在 htacess 中编写一个多重重写函数。

  • 将http改成httpS
  • 删除 www
  • 删除 .html

它应该处理所有情况:
http://www.example.com/about.html
应该改写为
http S://example.com/about

当然,如果只有一项需要重写,它也应该可以工作:
http://example.com/about
应该改写为
http S://example.com/about

这是我的代码(https 和 no-WWW 有效,但注释的 no-.html 无效)。
请注意,在重写时,您需要使用之前重写的 URL,而不是用户在地址栏中输入的内容,否则该规则将覆盖另一个规则所做的更改。

RewriteEngine on

#strip the www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

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

#switch to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

# Disable the server signature
ServerSignature Off

【问题讨论】:

    标签: html .htaccess mod-rewrite https no-www


    【解决方案1】:

    你可以使用:

    RewriteEngine on
    
    #strip the www
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
    
    #switch to https
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
    
    #remove .html if any
    RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.html[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # otherwise forward it to index.php
    RewriteRule . index.php
    
    # Disable the server signature
    ServerSignature Off
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 2015-01-10
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2014-07-26
      • 2017-05-03
      • 2018-11-12
      相关资源
      最近更新 更多