1

我正在尝试在 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
4

1 回答 1

0

您可以使用:

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
于 2014-03-24T19:19:25.477 回答