首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache重写规则

问Apache重写规则
EN

Stack Overflow用户
提问于 2011-12-07 23:19:44
回答 1查看 236关注 0票数 0

在Apache手册的指导下,我或多或少地得到了基本的Apache配置,但是此时重定向规则对我来说有点复杂,所以我希望您能帮助我。

我要做到以下几点:

  • 通过http传入的每个请求都应该被重定向为使用https。
  • 每个子网(除了定义为vhost的子网(m.domain.tld和content.domain.tld) )都应该重定向到domain.tld。
  • i应该保留"/“之后的所有内容,但这是不必要的。

例如:

  • http:/domain.tld→https:/domain.tld
  • http:/whatever.domain.tld→https:/domain.tld
  • https:/whatever.domain.tld→https:/domain.tld
  • http:/m.domain.tld→https:/m.domain.tld

我已经掌握了使用这个非常基本的规则的基本知识,但是它只适用于通过http传入的请求,因此通过https引入的非现有子网不会被重新格式化。

代码语言:javascript
复制
<VirtualHost _default_:80>
    Redirect permanent / https://domain.tld/
</VirtualHost>

希望你能帮助或指点我的方向一个明确的指南(还没有找到一个)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-08 00:26:42

尝试以下mod_rewrite规则:

代码语言:javascript
复制
RewriteEngine On
Options +FollowSymLinks

# force HTTPS for m.example.com
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} =m.example.com
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# only allow m.example.com & example.com -- everything else gets redirected to https://example.com
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !=m.example.com
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]

# if still no HTTPS then force it (for any other subdomain)
RewriteCond %{HTTPS} !=on
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]

# your other rewrite rules below

请记住,为了能够服务器something.example.com,您需要为特定的子域提供适当的SSL证书,或者需要覆盖任何子域的通配符SSL证书。这一点很重要,因为HTTPS的SSL部分在mod_rewrite有机会做任何事情之前就开始工作了(只有当安全连接已经正确建立时,它才会启动)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8424057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档