Regex 如何将url http重写为https

Regex 如何将url http重写为https,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,我正在.htaccess中使用此代码,但它不起作用。并且没有发生错误 RewriteEngine On RewriteCond %{SERVER_PORT} ^80$ RewriteRule ^.*$ https://%{localhost}%{REQUEST_URI} [R=301,L] 要将url从http自动重写为https,只需执行此操作并将domain.com更改为域TLD: RewriteEngine On RewriteCond %{SERVER_PORT}

我正在.htaccess中使用此代码,但它不起作用。并且没有发生错误

 RewriteEngine On
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule ^.*$ https://%{localhost}%{REQUEST_URI} [R=301,L]

要将url从http自动重写为https,只需执行此操作并将domain.com更改为域TLD:

RewriteEngine On

RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.domain.com/$1 [r=301,nc,L]

使用
%{localhost}
是个问题,因为没有名为
%{localhost}
的变量

在root.htaccess或Apache服务器配置中尝试此规则:

RewriteEngine On

RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{ENV:protossl} !=s [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !=on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

请将您的标记更改为
.htaccess
@HddnTHA哪个标记?您的问题有
php
标记,但它不包含任何关于php的内容。@HddnTHA您知道我问题的答案吗?如果我知道,我将为您的问题写一个答案。