https redirect with an exception (errors) - apache

I need to redirect all http requests to https except /sports-scores, /sport-scores/, and sport-scores.html pages.
I've been at it for two hours. My brain is fried and I can't make sense any sense from this.
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
RewriteEngine on
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost 127.0.0.1:443>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
SSLEngine on
SSLCertificateFile /home/user/gandi-ssl/www.crt
SSLCertificateKeyFile /home/user/gandi-ssl/server.key
SSLCertificateChainFile /home/user/gandi-ssl/www.crt
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

I need to redirect all http requests to https except /sports-scores,
/sport-scores/, and sport-scores.html pages.
You need to adjust your 80 Virtual Host config and use the below rewrite rule.
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
RewriteCond %{HTTPS} !^on
RewriteCond %{REQUEST_URI} !^/sports-scores
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost 127.0.0.1:443>
DocumentRoot /var/www
ServerName www.my-domain.com
ServerAlias my-domain.com
DirectoryIndex index.php index.html
SSLEngine on
SSLCertificateFile /home/user/gandi-ssl/www.crt
SSLCertificateKeyFile /home/user/gandi-ssl/server.key
SSLCertificateChainFile /home/user/gandi-ssl/www.crt
CustomLog /var/log/apache2/www.log combined
ErrorLog /var/log/apache2/errors-www.log
RewriteEngine On
RewriteCond %{HTTPS} ^on
RewriteCond %{REQUEST_URI} ^/sports-scores
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
Let me know how this works out.

Related

non www to www apache2 Letscrypt certbot

I want to redirect my non-www to www .
SSL working fine and both url working fine with ssl.
https://example.com
https://www.example.com
both working but I want to redirect https://example.com to https://www.example.com
I am working with lamp-server in AWS ec2 and using certbot for ssl.
My apache config.
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin admin#example.com
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I tried many online tutorials but nothing helped, Thanks in advance for any help or support.
Try (note: Apache may throw errors if the comments starting with # are not removed):
# turns on the rewrite engine
RewriteEngine On
# checks if domain is not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
# redirects to www.example.com
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# the above is enough for 443 VirtualHost
# checks if https is not on
RewriteCond %{HTTPS} !on
# redirects to https on the same domain
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So, your full configuration:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin admin#example.com
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
Add the following to the <VirtualHost *:443> configuration:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Non SSL to SSL and WWW to NON www at the vhost config

I have a vhost config that works perfectly for SSL.
Then I added this to redirect from non SSL to SSL.
<VirtualHost *:80>
ServerName example.com
ServerAdmin mail#example.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
This works perfectly too. But now I need a config to redirect www to non www.
So I mean like this:
https://www.example.com => https://example.com
http://www.example.com => https://example.com
I tried it like this:
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin mail#example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)*(.*)$ [NC]
RewriteRule ^/(.*)$ https://%2/$1 [R=301,NC,L]
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
ServerAdmin mail#example.com
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)*(.*)$ [NC]
RewriteRule ^/(.*)$ https://%2/$1 [R=301,NC,L]
</VirtualHost>
But when I tried to check it with curl, I get always the anwser that http://www.example.com was redirected to https://www.example.com.

Two domains pointing on same site even though it shouldn't

I've been trying to host 2 websites on my Ubuntu 18.04 server.
The two domains are pointing to the first site (Bilbao-deco).
As you can see on my.conf file, my two document root are different.
I can't tell what I did wrong, I'll be glad if anyone could help !
<VirtualHost *:80>
ServerName bilbao-deco.fr
ServerAlias bilbao-deco.fr
ServerAdmin nicolasalric#hotmail.fr
DocumentRoot /var/www/html
<Directory /var/www/html>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteCond %{SERVER_NAME} =bilbao-deco.fr
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName api.nalric.fr
ServerAlias api.nalric.fr
ServerAdmin nicolasalric
DocumentRoot /var/www/api/www/api_tub_ws/html
RewriteEngine on
RewriteCond %{SERVER_NAME} =api.nalric.fr
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Update : my problem were caused by certbot, it made another conf file, which is not the default one !

Apache redirect https non-www to https www (Ubuntu)

I know question like this has been posted several times. But I am facing a unique problem and looking for help regarding that.
I want to redirect everything to https://www.example.com
I configured LAMP on Ubuntu 16 and installed Lets Encrypt SSL. I also configured redirection and following is the status:
http non-www and http www are getting properly redirected to https www
https non-www and https www are both getting reverse redirected to https non-www (instead of intended https www)
Following are my apache configs:
1. 000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName www.example.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias example.com
SSLCertificateFile /etc/letsencrypt/live/getcreed.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/getcreed.com/privkey.pem
</VirtualHost>
</IfModule>
2. 000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>
Please let me know what I am doing wrong. Any help will be very helpful.
This makes all redirects to https://www. I am using my website for instance.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?itechplanetpc\.com
RewriteRule ^(.*)$ https://www.itechplanetpc.com/$1 [R=301,L]

Redirecting traffic in apache-centos

I am trying to redirect all http traffic to https
So I want these 3 scenarios to be true:
1- http://domain.com -> https://www.domain.com
2- http://www.domain.com -> https://www.domain.com
3- https://domain.com -> https://www.domain.com
I managed to have the first 2 working but the last is not working. How can i achieve that?
Below is my vhost config.
<virtualhost *:80>
RailsEnv production
ServerName www.domain.com
ServerAlias *.domain.* domain.* domain.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
RewriteCond %{REQUEST_METHOD} ^TRACK
RewriteRule .* - [F]
#redirect all port 80 traffic to 443
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R]
</IfModule>
<directory /data/project/current/public>
AllowOverride all
Options -MultiViews
</directory>
</virtualhost>
<virtualhost *:443>
RailsEnv production
ServerName www.domain.com
ServerAlias *.domain.* domain.* domain.com
SSLEngine on
SSLCertificateFile /root/cert.crt
SSLCertificateKeyFile /root/cert.key
SSLCertificateChainFile /root/certCA.crt
DocumentRoot /data/project/current/public
<directory /data/project/current/public>
AllowOverride all
Options -MultiViews
</directory>
</virtualhost>