Apache Redirects and slash problems - apache

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]

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.

Naked domain automaticly redirects but www not

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>

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.

Force SSL and WWW on apache2 for raspbian

When I use the mod_rewrite and mod_ssl concurrently below, the resulting domain is: https://www.domain.com// I find this extra '/' annoying and I would like to get rid of it. Is the problem the way I code it or should I consider using .htaccess instead?
000-default:
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.fusioncodes\.com [NC]
RewriteRule ^(.*)$ https://www.fusioncodes.com/$1 [R=301,L]
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://www.%{HTTP_HOST}/$1 [NC,R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
SSLEngine On
SSLCertificateFile ~server.crt
SSLCertificateKeyFile ~server.key
..... More SSL Stuff
</VirtualHost>
Change the 3rd line (RewriteRule) with that:
RewriteRule ^/?(.*)$ https://www.fusioncodes.com/$1 [R=301,L]
Refer here RewriteRule ^/?(.*) https://www.%{HTTP_HOST}/$1 [NC,R=301,L]

Problem with Apache rewrite rules. I want to redirect .com traffic to .co.uk

This is my current vhost.conf file:
<VirtualHost *:80>
DocumentRoot /data/sites/mysite
ServerName mysite.co.uk
ServerAlias www.mysite.co.uk
ServerName mysite.com
ServerAlias www.mysite.com
RewriteEngine On
RewriteOptions inherit
RewriteCond %{HTTP_HOST} !^www\.mysite\.co\.uk|^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^mysite\.co\.uk|^mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.mysite.co.uk/$1 [L,R]
CustomLog /var/log/httpd/mysite.log combined
</VirtualHost>
But I cannot get it to work properly. www.mysite.com is showing the mysite.co.uk website but the .com domain is still showing in the browser. Also if I go to www.mysite.com/myfiles it cannot find the folder that works on www.mysite.co.uk/myfiles
Any ideas?
Try this on the .com domain to redirect all requests to the co.uk
RewriteEngine On
RewriteRule ^(.*) http://yoursite.co.uk/$1 [R=301,L]