Skip to content
Advertisement

RewriteCond works sometimes

This is my code on /etc/apache2/sites-enabled/dynamic-vhosts.conf:

<VirtualHost *:80>            
            DocumentRoot "/var/www/SITE.com.br"
            ServerName SITE.com.br

                RewriteEngine On
                RewriteCond %{REMOTE_ADDR} 10.10.10.[0-255]
                RewriteRule ^(.+)$ https://SITE.com.br/ [R=301]

My problem is, in some cases it’s force to HTTPS, but in other cases it’s not. Example: On some desktops browsers, it’s redirect to HTTPS, but on mobile browsers or on others desktops/servers browsers, it’s not.

Anybody can help me?

Advertisement

Answer

Try this, but I am not so sure that this will fix your issue:

<VirtualHost *:80>            
            DocumentRoot "/var/www/SITE.com.br"
            ServerName SITE.com.br

                RewriteEngine On
                RewriteCond %{REMOTE_ADDR} ^10.10.10.
                RewriteRule ^(.+)$ https://SITE.com.br/ [R=301]

I am not really sure why you are using RewriteCond %{REMOTE_ADDR}, do you know that it actually makes the HTTP to HTTPS redirect happen only on remote IP addresses (the IP address of the client) from 10.10.10.0 to 10.10.10.255? And there is a very huge possibility that the mobile phones have IP addresses that are outside 10.10.10, like 10.10.11.123!

If you want to redirect to HTTPS for any IP address, try this:

<VirtualHost *:80>            
            DocumentRoot "/var/www/SITE.com.br"
            ServerName SITE.com.br

                RewriteEngine On
                RewriteRule ^(.+)$ https://SITE.com.br/ [R=301]

You can also simplify ^(.+)$ in RewriteRule ^(.+)$ https://SITE.com.br/ [R=301] to RewriteRule ^ https://SITE.com.br/ [R=301]. Since ‘^(.+)’ means “match and store everything that is in the current line”, it uses more memory (RAM), ^ doesn’t really store anything in memory.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement