Reputation: 1
I realize this is a question that was answered a lot but the other solutions are not working for me for some reason. I am trying to rewrite http as https but I am still getting 'Bad Request' when I try to browse my website using http.
Here is the code in my .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have AllowOverride All in the VirtualHost config and I made sure mod_rewrite is enabled.
Any help is appreciated. Let me know if you need more information.
Edit:
Here is my complete .htaccess file:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
POSSIBLE Solution: I was using Apache 2.2. This may be a bug with the older version of Apache. I am going to update and I will post again when I know more.
Upvotes: 0
Views: 1129
Reputation: 966
First of all you should be sure of enable mod_rewrite
and ssl
on your apache, by this codes
sudo a2enmod rewrite
sudo a2enmod ssl
and then restart your apache
sudo service apache2 restart
now follow this steps
sudo nano /etc/apache2/sites-enabled/000-default.conf
and add this codes
<VirtualHost *:80>
ServerName my.site.com
ServerAlias my.site.com
DocumentRoot /var/www/SITE-PATH
<Directory /var/www/SITE-PATH>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
then go to your website directory /var/www/SITE-PATH
and add this code to your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
and for your ssl credit on path /etc/apache2/sites-enabled/default-ssl.conf
you should have something like this
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
ServerAdmin admin@example.com
ServerName my.site.com #important
ServerAlias my.site.com #important
DocumentRoot /var/www/SITE-PATH #important
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt #important
SSLCertificateKeyFile /etc/apache2/ssl/apache.key #important
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
if you go all steps right, you should have this at end
http://my.site.com
=> redirect to => https://my.site.com
EDIT: for more detail on SSL please follow How To Create a SSL Certificate on Apache for Ubuntu 14.04 and this post
Upvotes: 1