首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >htacess:三重重写HTTPS + no-WWW + no-.html

htacess:三重重写HTTPS + no-WWW + no-.html
EN

Stack Overflow用户
提问于 2014-03-24 19:01:53
回答 1查看 80关注 0票数 1

我试图用htacess编写一个多重重写函数。

  • 将http更改为httpS
  • 删除www
  • 删除.html

它应处理所有案件:

http://www.example.com/about.html

应该重写为

http ://example.com/约

但当然,如果只有一个项目需要重写,它也应该有效:

http://example.com/about

应该重写为

http ://example.com/约

下面是我的代码(https和no-WWW工作,但是注释的no-.html不起作用)。

请注意,在重写时,您需要使用先前重写的URL,而不是用户在地址栏中键入的内容,否则规则将覆盖另一条规则所做的更改。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-24 19:19:25

您可以使用:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22618299

复制
相关文章

相似问题

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