Using Apache .conf for www to non www redirect - redirect

Using other Stack Overflow help questions I have tried to setup a way to redirect https and www. to my https:example.com site. I have the https redirect working but it isn't going from https://www.example.com to https://example.com.
http:www.example.com -> https:example.com ----works
http:example.com -> https:example.com ----works
https:example.com -> https:example.com ----works
https:www.example.com -> https:example.com ---- Doesnt work
Here is my config file
<VirtualHost *:80>
ServerName example.com
ServerAdmin example#gmail.com
DocumentRoot /var/www/html/example/public
<Directory /var/www/html/example>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Redirect / https://example.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
What am I doing wrong?

Related

Getting Error 400 Bad Request Error when trying to load non-www website

I'm using Apache2. My server's website (Linode) loads normally only if I include the www.[name].com link. If I use [name].com without the www, I actually receive a page, but it reads the 400 Bad Request Error. Here is my etc/apache2/sites-available files:
website.conf
<VirtualHost *:80>
ServerName website.com
ServerAlias www.website.com
Redirect 301 / https://www.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Here is my website-le-ssl.conf file (for HTTPS/SSL):
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/name/website/static
<Directory /home/name/website/static>
Require all granted
</Directory>
Alias /media /home/name/website/media
<Directory /home/name/website/media>
Require all granted
</Directory>
<Directory /home/name/website/website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/name/website/website/wsgi.py
WSGIDaemonProcess django_app python-path=/home/name/website/ python-home=/home/name/website/venv
WSGIProcessGroup django_app
SSLCertificateFile /etc/letsencrypt/live/name.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/name.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
In addition, I do not use .htaccess file, I do not use any other redirect file, and I have already enabled the a2enmod rewrite module. This site sometimes works without the non-www, but there are brief periods of time where it doesn't work and I'm confused as to why it acts this way. Is there a way to get Apache2 to redirect from non-www addresses to www addresses, and how do I make the redirect work ALL the time. Thank you!
Things tried:
I've tried using the RewriteEngine and mod_rewrite on the .htaccess file and I've tried completely restoring everything to default and reconfiguring it all from scratch and tutorials.

Reset project directory of apache web server virtual host having let's encrypt ssl certificate installed

I have a website www.example.com that is hosted on apache2 web server in /var/www/example.com directory and the virtual host config file is
<VirtualHost *:80>
ServerAdmin admin#gmail.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public
<Directory /var/www/example.com/public/>
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 >
</IfModule>
</VirtualHost>
I have installed let's encrypt certificate for this domain.
Now I have to change configuration settings and the config file should be like this:
<VirtualHost *:80>
ServerAdmin admin#gmail.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/dist //here is the change
<Directory /var/www/example.com/dist/> //here is the change
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 >
</IfModule>
</VirtualHost>
I have edited the config file and ran command certbot --apache -d example.com -d www.example.com.
Chose reinstall and renew both options and the installation was successful in both cases. But when I go to example.com then it shows 404 error.
How can I solve my problem?
HTTPS uses port 443, not port 80. Port 443 is closed. You need to add a new virtual host to handle HTTPS request
<VirtualHost *:443>
ServerName example.com
#ServerAlias www.example.com
ServerAdmin admin#gmail.com
DocumentRoot /var/www/example.com/dist
LogLevel debug ssl:info
SSLEngine on
SSLCertificateFile /path/to/yout/cert
SSLCertificateKeyFile //path/to/yout/key
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
If you check ports.conf under /etc/apache2, you will see this:
<IfModule ssl_module>
Listen 443
</IfModule>
Apache2 will open port 443 when the SSL module is enabled. So remember to run:
sudo a2dismod ssl
sudo systemctl restart apache2

Setting Up Virtual hosts for XAMPP on windows

XAMPP for Windows 1.8.2 PHP 5.4,
Operating System : windows xp
I am using username and passowrd on XAMPP
c:\windows\system32\drivers\etc\hosts
127.0.0.1 hindustans.local
127.0.0.1 read.local
E:\My Projects\wesites\read
index.html
httpd-vhosts.conf
<Directory "E:/My Projects/wesites/read">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "E:/My Projects/wesites/read"
ServerName read
Options +Indexes
</VirtualHost>
read.local works fine with c:\xampp\htdocs
But
read.local doesn't work with E:\My Projects\wesites\read\index.html
I brief want to run my project from E:\My Projects\wesites\read
You need match the ServerName to the address you have in hosts and give it permission
<VirtualHost *:80>
DocumentRoot "E:/My Projects/wesites/read"
ServerName read.local
<Directory "E:/My Projects/wesites/read" >
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
then try to access the site at read.local.
More info here

zf2 virtual host loading localhost/xampp

I'm trying to get zf2 running on my xampp.
I have this vhosts.conf file
NameVirtualHost 127.0.0.1:80
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName zf2-tutorial.localhost
DocumentRoot "C:/xampp/htdocs/zf2-tutorial"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/zf2-tutorial">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
and this host file
127.0.0.1 zf2-tutorial.localhost
Now, when I run zf2-tutorial.localhost, the browser goes to zf2-tutorial.localhost/xampp and shows xampp page.
When I run localhost it goes to localhost/xampp too.
Please help.
Try http://zf2-tutorial.localhost/public
If that works you could change the following entries in your vhosts file
DocumentRoot "C:/xampp/htdocs/zf2-tutorial/public"
<Directory "C:/xampp/htdocs/zf2-tutorial/public">

Apache redirects www.domain.com to domain.com/domain

I'm preparing my ubuntu server in Amazon AWS to host a new website. It uses Apache2. I've done this before with no problem whatsoever, for example, to trick a friend I spoofed google.com and www.google.com (editing his hosts file) and everything worked fine with and without 'www.'.
Now I have bought a domain name 'domain.com' with 1&1 , I have configured a subdomain 'www.domain.com' and I have pointed it to my server. The dns record is fine, I checked. I have also edited my sites-enabled default file and added these entries:
<VirtualHost *:80 >
ServerName www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerName domain.com
DocumentRoot /var/www/domain
</VirtualHost>
When I visit domain.com in my browser it works fine, but when I visit www.domain.com I get domain.com/domain/ and obviously a 404.
I used telnet and discovered that the server sends a 301 when you request the root page of host: www.domain.com
I don't know why this is happening, I've tried lots of modifications to the configuration above like:
<VirtualHost *:80 >
ServerAlias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerAlias domain.com
DocumentRoot /var/www/domain
</VirtualHost>
.
<VirtualHost *:80 >
ServerName domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerName www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
.
<VirtualHost *:80 >
ServerName domain.com
Server Alias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
but it kept happening.
For the record, this is what I used for the spoof that still works fine
<VirtualHost *:80 >
ServerName google.com
DocumentRoot /var/www/google
</VirtualHost>
<VirtualHost *:80 >
ServerName www.google.com
DocumentRoot /var/www/google
</VirtualHost>
I've been searching for two days now, but all I find is the same configuration I'm using and no explanation about why this isn't working.
Thanks for your help!
PS: There are no .htaccess files in this directory or in any of the parent directories.
In case anybody enters this question.
I still don't know what was wrong but after moving these configurations around the file they've magically started working normally.
Thanks to everybody who took the time to read the question.
Its better to put
<VirtualHost *:80 >
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
You can't put a space between Server and Alias so I changed to show you
Hope It Works!