Home .htaccess rewrite https to www, but not when IP-address is used
Reply: 1

.htaccess rewrite https to www, but not when IP-address is used

Florian
1#
Florian Published in 2018-02-13 10:07:35Z

I have got a Nextcloud installed on my Raspberry Pi. Https is set up and the rewrite engine is on. Rewrite is also working so far. Now I have got the following problem: If I am invoking my cloud inside the LAN-network I have to use the internal IP-address (192.168.1.20) and using the Internet to reach the cloud over the browser I set up an URL.

For always having a proper URL I want to use a .htaccess-file (and later a IfModule rewrite) to rewrite the URL. The following cases should be rewritten:

http://example.com/xyz -> https://www.example.com/xyz
http://www.example.com/xyz -> https://www.example.com/xyz
http://192.168.1.20/xyz -> https://192.168.1.20/xyz

I am not very familiar with the syntax and so I tried the following without success:

RewriteEngine on

# for URL
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^192.168.1.20
RewriteRule ^https://www.example.com%{REQUEST_URI} [R=301,L,NE]

# for IP
#RewriteCond %{SERVER_NAME} ^192.168.1.20
#RewriteCond %{HTTPS} off
#RewriteRule ^https://192.168.1.20%{REQUEST_URI} [R=301,L,NE]

I would be glad if someone could help me...

EDIT: My "000-default.conf" definitions are:

<VirtualHost _default_:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ServerName www.example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost _default_:443>
  DocumentRoot /var/www/html
  ServerName 192.168.1.20
  <IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
  </IfModule>
  SSLEngine on
  SSLCertificateFile /etc/apache2/ssl/cloud.crt
  SSLCertificateKeyFile /etc/apache2/ssl/cloud.key
</VirtualHost>

And my "default-ssl.conf" definitions:

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html

        ServerName=www.example.com

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on

        SSLCertificateFile  /etc/apache2/ssl/cloud.crt
        SSLCertificateKeyFile   /etc/apache2/ssl/cloud.key

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>
    </VirtualHost>
</IfModule>
Florian
2#
Florian Reply to 2018-02-15 13:31:30Z

Meanwhile i solved the problem myself!

There were some syntax mistakes and some other problems. The following code inside a ".htaccess"-file at the root directory works for me:

RewriteEngine on

# for URL
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_NAME} !^192\.168\.1\.20
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L,NE]

# for IP
RewriteCond %{SERVER_NAME} ^192\.168\.1\.20
RewriteCond %{HTTPS} !=on
RewriteRule .* https://192.168.1.20%{REQUEST_URI} [R=301,L,NE]

The only remaining problem is that https://example.com is NOT corrected to https://www.example.com, but in my case this is not important...

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.537413 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO