Naked domain automaticly redirects but www not - apache

I want to redirect my traffic to HTTPS
Coming from myawesomesite.com it redirects to https://myawesomesite.com, and coming from http(s)://www.myawesomesite.com it goes to HTTP or HTTPS based on the call.
What's wrong?
My virtualhost file is:
<VirtualHost *:80>
DocumentRoot /var/www/myawesomesite/public
ServerName myawesomesite.com
ServerAlias www.myawesomesite.com
<Directory "/var/www/myawesomesite/public/">
AllowOverride All
Options FollowSymLinks
</Directory>
ErrorLog /var/log/apache2/myawesomesite-error_log
RewriteEngine On
RewriteCond %{SERVER_NAME} ="myawesomesite.com" [OR]
RewriteCond %{SERVER_NAME} ="www.myawesomesite.com"
RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI}
</VirtualHost>

This is the correct way to force SSL via Rewrite functions:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
Of course, remove your current code. Enjoy!
Update:
You can also do this which is better:
<virtualhost *:80="">
ServerName www.example.com
Redirect / https://www.example.com/
</virtualhost>

Related

website not showing when using ssl on mobile but shows on desktop

I have programmed a website which is : www.shoesbank.org
I've used following code to redirect to https. It works perfectly on desktop. But when i want to use it on mobile. I get error which says" the site can not be reached". When i remove https redirection, it is shown perfectly. I have spent tones of hours googling for it, but i found no thing. I appreciate your help.
httaccess:
RewriteEngine On
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shoesbank.org [NC]
RewriteRule ^(.*)$ https://www.shoesbank.org/$1 [L,R=301,NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(support\.)?abc\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
When I config my apache server, I prefer to handle https in the apache config instead of in the .htacces.
<VirtualHost *:80>
ServerName shoesbank.org
ServerAlias www.shoesbank.org
Redirect / https://shoesbank.org
</VirtualHost>
<VirtualHost *:443>
ServerAdmin contact#shoesbank.org
DocumentRoot /var/www/shoesbank.org/public_html/
ServerName shoesbank.org
ServerAlias www.shoesbank.org
SSLEngine on
SSLCertificateFile /etc/ssl/shoesbank.org.crt
SSLCertificateKeyFile /etc/ssl/shoesbank.org.key
SSLCertificateChainFile /etc/ssl/shoesbank.org.ca-bundle
<Directory /var/www/shoesbank.org/public_html/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Hopefully It works.

Apache configuration for slim3 with ssl

I'm trying to setup my apache configuration file with my slim framework application to use ssl. I've done this in the past but for whatever reason, I'm getting 404 errors when I try to access any page other than the home page. Here is my apache configuration file:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{SERVER_NAME} =www.my-site.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ServerAdmin my.email#gmail.com
ServerName www.my-site.com
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/privkey.pem
ServerAdmin my.email#gmail.com
DocumentRoot /wwwroot/sites/my-site/public
ServerName www.my-site.com
ErrorLog logs/www.my-site.com-error_log
CustomLog logs/www.my-site.com-access_log common
<Directory "/wwwroot/sites/my-site/public">
AllowOverride All
RewriteEngine On
RewriteBase /wwwroot/sites/my-site/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Directory>
</VirtualHost>
As stated, my home page works exactly as I'd expect it to work. But when I try to go to https://www.my-site.com/another/page, I get a 404 error.
I have a dev version of this project set up on another server that doesn't use https and I have no problems going to http://dev.my-site.com/another/page.
try set RewriteBase to /
<Directory "/wwwroot/sites/my-site/public">
AllowOverride All
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Directory>

Redirect to https + www everywhere

I want my site to use "https://www" everywhere. non-www to www and non-https to https. Unfortunately I'm getting www.example.come redirected you too many times error.
my virtual host config:
<VirtualHost *:80>
DocumentRoot "D:/www"
ServerName www.example.come
<Directory "D:/www">
Options -MultiViews
AllowOverride All
Require all granted
Order Allow,Deny
Allow from 127.0.0.0/8
</Directory>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName example.come
Redirect permanent / https://www.example.come/
</VirtualHost>
If i didn't include below lines
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
It will redirect: example.com > https://www.example.com
but wont redirect www.example.com won't be redirect to https.

Apache2 conf Redirect everything to HTTPS except one path

I'm trying to redirect everything on my WordPress-site to HTTPS except one url, http://example.com/woocommerce/callback/ but I cant get it to work.
This is my apache config:
<VirtualHost *:80>
ServerName example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !woocommerce/callback
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
DocumentRoot /mnt/persist/www/
<Directory /mnt/persist/www/>
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
But it still redirects to HTTPS for http://example.com/woocommerce/callback/

Apache Redirects and slash problems

I have a rewrite set up that directs a second domain to a first domain via a 301. However, whenever I go to seconddomain.com or www.seconddomain.com in my browser, I end up at http://www.firstdomain.com//
Notice the two slashes?!? Driving me crazy. How can I avoid this?
<VirtualHost *:80>
ServerName www.firstdomain.com
ServerAlias firstdomain.com *.firstdomain.com
DocumentRoot /opt/firstdomain.com/static
<Directory /opt/firstdomain.com/static>
Allow from all
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.seconddomain.com
ServerAlias seconddomain.com *.seconddomain.com
RewriteEngine On
RewriteRule ^(.*)$ http://www.firstdomain.com/$1 [R=301,L]
</VirtualHost>
This is happening because the URI's that are used in a virtualhost context has a leading slash, unlike URI's that are passed through rules in an htaccess file. You need to change your rule to:
RewriteRule ^/(.*)$ http://www.firstdomain.com/$1 [R=301,L]