【问题标题】:htaccess help, need to force www, https, and remove index.phphtaccess帮助,需要强制www,https,并删除index.php
【发布时间】:2011-10-14 10:16:58
【问题描述】:

我在我的 htaccess 文件中进行了重写,从 url 中删除了 index.php

RewriteEngine on
RewriteCond $1 !^(images|media|system|themes|_css|_js|favicon\.ico|robots\.txt|cert\.html|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]

除此之外,我想对任何没有两者的请求强制使用 wwwhttps

所以最终所有的 url 应该是这样的:https://www.example.com/whatever/something/;并且出于 SEO 的目的,如果一个 url 错过了标记,它应该 301 重定向到它的正确版本,例如:

http://example.com/about/
301 redirect to
https://www.example.com/about/

希望得到一些帮助,谢谢!

【问题讨论】:

    标签: apache .htaccess mod-rewrite https litespeed


    【解决方案1】:

    强制万维网:

    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] # include 's' here to force ssl in addition to www
    

    强制 SSL:

    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
    

    删除“index.php”:

    RewriteCond %{THE_REQUEST} /index.php HTTP
    RewriteRule (.*)index.php$ /$1 [R=301,L]
    

    【讨论】:

    • 谢谢,到目前为止,这似乎工作得很好。如果我在浏览器中点击domain.com,我会收到证书不受信任的错误,因为证书仅适用于 www。有什么办法吗?
    • @mjr 获取涵盖domain.comwww.domain.com 的证书 .. 或更昂贵的通配符证书(将涵盖所有子域)。不幸的是,必须先建立安全通道 (SSL),然后才能启动 HTTPS 的 HTTP 部分。换句话说,在连接完全建立之前,不会执行重写规则。
    • @mjr 我提交了对已接受答案的修改,应该可以解决您的问题。
    • 当键入www.example.com/example 之类的内容时,第一个代码 sn-p 不会强制 SSL。 “强制 SSL:”下的第二个代码 sn-p 似乎对我来说非常有效。
    猜你喜欢
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 2017-01-08
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2016-03-15
    相关资源
    最近更新 更多