Redirect all to https://www.example.com - apache

OK, so I know that nearly every variety of this question has been asked and answered on this forum, and many others, but I just can't seem to get it right. My hope is that if I provide enough specifics, including the process I'm following, that someone will be able to spot my error and correct me. Here goes!
The Specifics
Drupal multi-site
Apache web server
Sites provisioned by Aegir
What I want to accomplish vs what I have accomplished
I want to direct all http and https traffic to https://www.example.com. The following examples marked with a + have been accomplished; the one marked with - eludes me:
+ http://example.com -> https://www.example.com
+ http://www.example.com -> https://www.example.com
+ https://www.example.com -> https://www.example.com #redundant, but at least we know it works
- https://example.com -> https://www.example.com
What I'm doing
Each site has a vhost file generated by Aegir, and each of these vhost files contains two VirtualHost directives:
<VirtualHost 0.0.0.0:443>
<VirtualHost *:80>
I've modified example.com's vhost file to include the following code under the <VirtualHost *:80> directive, which has successfully achieved the three working examples above:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
The Problem
What I can't seem to figure out is how to enact the https://example.com -> https://www.example.com rewrite. I've tried many examples found on the Internet under both the <VirtualHost 0.0.0.0:443> and <VirtualHost *:80> directives with no luck.
Also, I'm reloading Apache and clearing my browser's cache after each edit of the vhost file. Any insight, guidance or correction would be much appreciated.
Thanks to all!
Edit:
Below is a redacted version of my vhost file:
<VirtualHost 0.0.0.0:443>
DocumentRoot /var/www/drupal
ServerName example
SetEnv db_type mysql
SetEnv db_name example
SetEnv db_user example
SetEnv db_passwd 1234567
SetEnv db_host somedb
SetEnv db_port 1234
# Enable SSL handling.
SSLEngine on
SSLCertificateFile /ssl.d/example/example.crt
SSLCertificateKeyFile /ssl.d/example/example.key
SSLCertificateChainFile /ssl.d/example/example_chain.crt
ServerAlias example.com
ServerAlias www.example.com
<IfModule mod_rewrite.c>
RewriteEngine on
# this isn't working; neither has anything else that I've tried here.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Extra configuration from modules:
# Error handler for Drupal > 4.6.7
<Directory "/var/www/drupal/sites/example/files">
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
# Prevent direct reading of files in the private dir.
# This is for Drupal7 compatibility, which would normally drop
# a .htaccess in those directories, but we explicitly ignore those
<Directory "/var/www/drupal/sites/example/private/" >
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Deny from all
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/drupal
ServerName example
SetEnv db_type mysql
SetEnv db_name example
SetEnv db_user example
SetEnv db_passwd 1234567
SetEnv db_host somedb
SetEnv db_port 1234
ServerAlias example.com
ServerAlias www.example.com
<IfModule mod_rewrite.c>
# these rules work for everything except:
# https://example.com -> https://www.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
# Extra configuration from modules:
# Error handler for Drupal > 4.6.7
<Directory "/var/www/drupal/sites/example/files">
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
# Prevent direct reading of files in the private dir.
# This is for Drupal7 compatibility, which would normally drop
# a .htaccess in those directories, but we explicitly ignore those
<Directory "/var/www/drupal/sites/example/private/" >
<Files *>
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Files>
Deny from all
Options None
Options +FollowSymLinks
# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
</VirtualHost>

OK now I get it with the content of your virtualhost.
This is not valid:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Actually you can't use %{HTTP_HOST} or %{REQUEST_URI} in the RewriteRule part
That's why it's redirecting you to a non-existent host, hence the connection refused error.
You should use this rule in the ssl virtualhost:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]
And this one in the non ssl one:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com$1 [R=301,L]
HTH

Related

forcing Codeigniter HMVC to use SSL

I have a website build with Codeigniter HMVC. On my local machine using Wamp it runs like it should. I want to upload it to my webserver that has an SSL certificate.
No matter what i try, i cant get the website to run on my webserver. I have tried following this :
How to force ssl in codeigniter?
But none of the given solutions work in my situation.
my .htaccess file looks like this :
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
in config.php of codeigniter i have :
$config['base_url'] = 'https://www.example.com/';
my webserver is an ubuntu 16 machine with apache, using an letsencrypt certificate. mod rewrite is enabled, but besides that is a standard configuration.
I have tried redirecting with hooks as mentioned in the link above, but to no avail. I have also tried various .htaccess file configuration but also no result there.
Is there anyone who has this running on their server?
regards,
sander
Because you say,
my webserver is an ubuntu 16 machine
I assume you are not using a shared server from a hosting provider and so you have complete access to Apache's configuration files. If that is true then you should not use .htaccess files. Read THIS and THIS to learn why.
I also assume you have a set of Apache "sites-available" files that define <VirtualHost> configs. It is inside those <VirtualHost> blocks where you should be placing the code you currently have in .htaccess.
To redirect all http requests to https try this as the contents of the "default" site in a "sites-available" configuration file e.g. "000-default.conf"
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
RedirectMatch 301 (.*) https://www.example.com$1
</VirtualHost>
What this will do is take any http request and immediately redirect to the https: URL. If you are using some other port besides the typical :80, adjust accordingly.
As mentioned, you can do the rewriting in a VirtualHost. Here's an example for the https: config file (maybe named along the lines of "020-example-443.conf")
<VirtualHost *:443>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/whatever
ServerAdmin web-boss#example.com
<Directory /var/www/whatever>
DirectoryIndex index.php
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [PT]
</Directory>
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
# Certificates delivered by certbot - your's maybe elsewhere
SSLCertificateFile /etc/letsencrypt/live/yoursite/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yoursite/privkey.pem
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
With these in place, you won't have to do anything special in CodeIgniter except, as you already do, use the following
$config['base_url'] = 'https://www.example.com/';
You must add after RewriteEngine on
<IfModule mod_ssl.c>
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

.htaccess multiple domains

I have one host for two domain. The first domain and its rules (aaaaa.com) are working.
When I try to append rules of second one (bbbbb.com) send an Internal Error 500 message.
The goal is bbbbb.com should reply the content of bbbbb directory. Please help..
Options -Indexes
<IfModule mod_rewrite.c>
ErrorDocument 404 /errors/error404.html
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www\.)(?!aaaaa\.com)[^\.]+)\.
RewriteRule ^(.*)$ aaaaa/index.html?p=%1 [L]
RewriteCond %{HTTP_HOST} ^bbbbb\.com [NC]
RewriteRule ^(.*)$ bbbbb/%1 [R,L]
</IfModule>
If you have access to the apache configuration, I recommend you to use Virtual Hosts to achieve your purpose.
You should include this configuration in your /etc/apache2.conf file:
<VirtualHost *:80>
DocumentRoot "/www/aaaaa"
ServerName aaaaa.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/bbbbb"
ServerName bbbbb.com
</VirtualHost>
Supposing that your web directories are stored in /www.
You can find the documentation here:
https://httpd.apache.org/docs/current/vhosts/

Apache: difference virtualhost and htaccess

I set up a private server (raspberry) with apache.
the config file for the website:
<VirtualHost *:80>
ServerName me.example.com
ServerAlias me.example.com
RewriteEngine On
#only rewrite if not one of the followed files is requested
RewriteCond %{REQUEST_URI} !^/images/.*\.(jpg|ico|png|mp4|ogg)
RewriteCond %{REQUEST_URI} !^/css/.*\.css
RewriteCond %{REQUEST_URI} !^/js/.*\.js
#rewrite all requests to index.php and get path (only intern)
RewriteRule ^/([^/]*)/(.*)$ /index.php?lang=$1&path=$2 [NC]
DocumentRoot /var/www/my/root
<Directory /var/www/my/root>
Options FollowSymLinks
Options -Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
1.) If I request now a "subpage" like /impressum.php. It will show me the impressum, instead of rewriting to the index file. Why?
2.) Now I copied the part of the rewrite engine into a .htaccess file and it doesn't work anymore. Where is the difference between these two files? What do I need to change, so it will work?
My .htaccess file looks like:
Options +FollowSymLinks
RewriteEngine On
#only rewrite if not one of the followed files is requested
RewriteCond %{REQUEST_URI} !^/images/.*\.(jpg|ico|png|mp4|ogg)
RewriteCond %{REQUEST_URI} !^/css/.*\.css
RewriteCond %{REQUEST_URI} !^/js/.*\.js
#rewrite all requests to index.php and get path (only intern)
RewriteRule ^/([^/]*)/(.*)$ /index.php?lang=$1&path=$2 [NC,L,QSA]
Thanks in advance
the files were missing ... so it redirected to the home.
It works better then me

.htaccess strange behaviour

If I put theese lines to .htaccess everything works well (when I'm going to http://www.example.ru I'm redirected to http://example.ru).
RewriteEngine On
# Installation directory
RewriteBase /
# Redirect all www to non-www
RewriteCond %{HTTP_HOST} ^www.example.ru [NC]
RewriteRule ^(.*)$ http://example.ru/$1 [L,R]
But If I put exactly same lines to <VirtualHost> -> <Directory> section trying to go to http://www.example.ru redirects me to http://example.ru/www.
Anybody knows why?
UPD
New VirtualHost:
<VirtualHost *:80>
ServerName example.ru
ServerAlias www.example.ru
ServerAdmin webmaster#localhost
DocumentRoot /home/example/www/example.ru/www
RewriteEngine On
<Directory /home/example/www/example.ru>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/example/www/example.ru/log/error.log
LogLevel warn
SetEnvIf Remote_Addr 127.0.0.1 loopback
CustomLog /home/example/www/example.ru/log/access.log combined env=!loopback
# Redirect all www to non-www
RewriteCond %{HTTP_HOST} ^www.example.ru [NC]
RewriteRule ^(.*)$ http://example.ru$1 [L,R]
</VirtualHost>
Spent some time I've figured out, that in VirtualHost section, pattern will match against the part of the URL after the hostname and port, and before the query string (in my case "/").
In Directory section, pattern will match against the filesystem path.

Per-server rewriterules not working

I've got a VirtualHost directive in /etc/apache2/openpanel.d/www.example.com.conf:
<VirtualHost *:80>
ServerAdmin "jon#example.com"
DocumentRoot /home/openpanel-admin/sites/www.example.com/public_html
ServerName www.example.com
ServerAlias example.com
<Directory /home/openpanel-admin/sites/www.example.com/public_html>
AllowOverride All
Allow from all
</Directory>
Include /etc/apache2/openpanel.d//www.example.com.inc/[^.#]*
Include /etc/apache2/openpanel.d//global.inc
</VirtualHost>
And I've the following in /etc/apache2/openpanel.d/www.example.com.inc/RewriteRules
<Directory /home/openpanel-admin/sites/www.stallfinder.com/public_html>
Options +FollowSymlinks
RewriteEngine On
RewriteRule agricultural-show-c780.html /search/event/agricultural-shows/1/ [R=301,L]
RewriteRule antique-fair-c596.html /search/event/antique-and-collectors-fairs/1/ [R=301,L]
RewriteRule baby-and-toddler-fairs-c896.html /search/event/baby-and-toddler-fairs/1/ [R=301,L]
RewriteRule book-fair-c631.html /search/event/book-fairs/1/ [R=301,L]
# etc... there are ~3000 of these
</Directory>
And I've got an .htaccess file in /home/openpanel-admin/sites/www.example.com/public_html:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Search pages
RewriteRule ^search/(stallholder|event)/?$ /find-$1.php [L]
RewriteRule ^search/(stallholder|event)/([^/]+)/([0-9]+)/? /$1.php?name=$2&id=$3 [L]
But the RewriteRules in the include file (RewriteRules) don't appear to be parsed/used.
The file is being included because I can put non-allowed stuff in there and apache will fail to load the config file, but if I turn on Apache redirect logging, then I only see [perdir] lines as if all the RewriteRules I've got in the RewriteRules include aren't being processed.
The global.inc file is empty, and the RewriteRules in my .htaccess file work fine.
Any clue what I'm doing wrong?
Try adding RewriteOptions directive into your .htaccess to allow executing rewrite rules on parent (upper) level.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions