我正在尝试在htacess中编写多重写功能。
它应该处理所有情况:
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
答案 0 :(得分: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