我找不到任何 100% 有效的东西
我想要 htaccess 规则
我知道每个人都认为我们以前见过这个,这是通常的回复,但是......
RewriteEngine on
## Force https and www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.test\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.test.co.uk/$1 [L,R=301]
## Remove trailing slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^(.*)/$ https://www.test2.co.uk/$1 [R=301,L]
这些是在https://htaccess.madewithlove.com/
中测试的结果http://test.co.uk
= https://test.co.uk/
- 不太担心这里的尾随 / https://test.co.uk
= https://www.test.co.uk/
- 好的https://test.co.uk/page1/
= https://www.test.co.uk/page1/
- 问题不想要尾随 /https://www.test.co.uk/page1/
= https://www.test2.co.uk/page1
- 效果很好不仅如此,我在 IONOS 中托管,并且出现以下行为...
当我去
https://www.test.co.uk/page1/
它实际上解析为https://www.test.co.uk//page1
- 我松开了尾部斜杠,但它显示为双???我没有主意,请帮忙
您必须避免多次重定向,以获得更好的 SEO 和更快的响应时间。
您可以在站点根目录 .htaccess 中使用这些规则:
RewriteEngine On
## remove www, trailing slash and turn on https in same external redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^ %1 [L,E=DSLASH:1]
RewriteCond %{ENV:REDIRECT_DSLASH} =1 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^:]+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
一旦确认其工作正常,请将
R=302
替换为 R=301
。在测试您的 R=301
规则时,避免使用 mod_rewrite
(永久重定向)。