如果指令在 apache VirtualHost conf 中不起作用,则显示“无效命令”

Not*_*aeL 8 scripting httpd.conf redirect virtualhost apache-2.2

为什么??

<VirtualHost *:80>
        ServerAdmin admin@mydomain.com
        DirectoryIndex index.php
        <If "%{SERVER_PROTOCOL} != 'HTTPS'">
            Redirect / https://www.mydomain.com:443/
        </If>
.....
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

保存,然后重启:

sudo /etc/init.d/apache2 restart
Syntax error on line 4 of /etc/apache2/sites-enabled/000-default:
Invalid command '<If', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.
   ...fail!
Run Code Online (Sandbox Code Playgroud)

The*_*Wiz 5

“如果”不是 Apache 理解的东西(在 2.3 版之前)。你可能应该看看 mod_rewrite

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://example.com:443/$1 [R,L]
Run Code Online (Sandbox Code Playgroud)

要找到您的 apache 版本,您可能可以使用: apache2 -v

  • &lt;If&gt; 来自 apache2.3。我怀疑您使用的是 2.2。http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost (3认同)