If directive not working in apache VirtualHost conf, says "Invalid command"

15,027

"If" is not something Apache understands (before version 2.3). You probably should look at mod_rewrite

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

To find your apache version you can probably use: apache2 -v

Share:
15,027

Related videos on Youtube

NotGaeL
Author by

NotGaeL

None of the opinions expressed on the content I post here necessarily reflect the opinions of my employers (or my current ones). The dumb ones are probably sarcastic. https://abstrusegoose.com/249

Updated on September 18, 2022

Comments

  • NotGaeL
    NotGaeL over 1 year

    Why??

    <VirtualHost *:80>
            ServerAdmin [email protected]
            DirectoryIndex index.php
            <If "%{SERVER_PROTOCOL} != 'HTTPS'">
                Redirect / https://www.mydomain.com:443/
            </If>
    .....
    </VirtualHost>
    

    Save, and then restart:

    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!
    
  • NotGaeL
    NotGaeL over 12 years
    Are you sure? The documentation says otherwise, it even suggests using it over Rewrite: httpd.apache.org/docs/2.3/rewrite/remapping.html#canonicalho‌​st
  • MastaJeet
    MastaJeet over 12 years
    <If> is from apache2.3. I suspect you are using 2.2. httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalho‌​st
  • NotGaeL
    NotGaeL over 12 years
    BTW, a small detail: The RewriteCond should be off (without =) not != on otherwise you get RewriteCond bad delimiters error. Tried to edit but something happens with serverfault it doesn't let me. Please do it yourself if you don't mind so the solution stays corrected for others. Thanks again :)
  • TheCompWiz
    TheCompWiz over 12 years
    Done. Although I believe != is completely valid. I'll have to double-check.
  • NotGaeL
    NotGaeL over 12 years
    it is, I was using != on (with a space in the middle). Dumb again!
  • TheCompWiz
    TheCompWiz over 12 years
    reverted back. :D