apache redrict 301 from https://www.domain.com to https://domain.com - apache

im trying for hours to redrict https:// www. domain .com to https:// domain .com with 301.
I have a certificate for domain .com but no wildcard for www. domain .com.
I tried the following:
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.meinedomain\.de$ [NC]
RewriteRule ^(.*) https://meinedomain.de/$1 [L,R=301]
and
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
and
Redirect 301 / https://mineyourmind.de/
None of this works. Any ideas what could be wrong? I have added this directly to the apache settings and it is working great for http:// www. domain .com and http:// domain .com to https:// domain .com but not for https:// www. domain .com.
At the moment I use the following config. Every redrict works fine, instead of the https://www.mineyourmind.de to https://mineyourmind.de
How should such a file look like ?
<VirtualHost *:80>
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
Redirect 301 / https://mineyourmind.de/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin **
DocumentRoot /var/www/mineyourmind
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
SSLEngine on
SSLCertificateFile /etc/**
SSLCertificateKeyFile /etc/**
SSLCertificateChainFile /etc/**
SSLCACertificateFile /etc/**
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
deny from all
</Directory>
<Directory /var/www/mineyourmind/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Second Edit:
<VirtualHost *:80>
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin **
DocumentRoot /var/www/mineyourmind
ServerName mineyourmind.de
ServerAlias www.mineyourmind.de
SSLEngine on
SSLCertificateFile /etc/apache2/**
SSLCertificateKeyFile /etc/apache2/ssl/**
SSLCertificateChainFile /etc/apache2/ssl/**
SSLCACertificateFile /etc/apache2/ssl/**
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
deny from all
</Directory>
<Directory /var/www/mineyourmind/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you request https://www.meinedomain.de/ and your cert isn't authorized for www.meinedomain.de, then you'll get a security warning regardless of how you redirect. The redirect can only happen after they accept the security exception.
Make sure you've put those rules in the SSL vhost's config, not the non-SSL one. Apache usually has them separate. If that still doesn't work, you can try just putting this in an htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.meinedomain\.de$ [NC]
RewriteRule (.*) https://meinedomain.de%{REQUEST_URI} [R=301,L]

Related

How to domain redirect in apache?

I'm trying to redirect domain-a to domain-b. So when the user types in domain-a, it will lead them to domain-b. I tried using an HTACCESS file and I'm not having any luck. So here is what I put in the server config file.
<VirtualHost *:80>
ServerAdmin support#domain-b.com
ServerName domain-a.com
ServerAlias www.domain-a.com
DocumentRoot /var/www/domain-a.com/public_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>
# Testing the rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-a\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteRule ^/?$ "http\:\/\/domain-b.com\.com\/" [R=301,L]
</VirtualHost>

Incorrect SSL-configuration for BookStack

after trying unsuccessfully to set up BookStack with nginx, I switched to Apache.
However, I have similar difficulties setting up the subfolder.
BookStack should be available at https://domain.name/bookstack.
The location of BookStack is /var/www/bookstack/... .
I have also added the domain in the .env.
I just figured out, that the configuration for HTTP works like expected.
<VirtualHost *:80>
ServerName domain.name
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# BookStack Configuration
Alias "/bookstack" "/var/www/bookstack/public"
<Directory "/var/www/bookstack/public">
Options FollowSymlinks
AllowOverride none
Require all granted
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
<Directory "/var/www/bookstack">
AllowOverride None
Require all denied
</Directory>
# End BookStack Configuration
</VirtualHost>
But when I put in the config for HTTPS (I created a certificate with certbot/letsencrypt), I get the login page without styles. As I log in, it takes me to HTTP-domain with error 404 Not found.
Here's my current SSL config:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName domainname
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# BookStack Configuration
Alias "/bookstack" "/var/www/bookstack/public"
<Directory "/var/www/bookstack/public">
Options FollowSymlinks
AllowOverride none
Require all granted
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
<Directory "/var/www/bookstack">
AllowOverride None
Require all denied
</Directory>
# End BookStack Configuration
SSLEngine on
SSLCertificateFile /path/to/certificate/file
SSLCertificateKeyFile /path/to/certificate/key/file
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I don't get the problem with this.

http://example.com would not redirect to https://www.example.com

we have this two conf on our webserver.
http:
<VirtualHost *:80>
ServerName www.example.de
ServerAlias example.de www.example.at example.at www.example.net example.net
RewriteEngine On
RewriteCond %{HTTP_HOST} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}
# Error page is just the index telling about the situation of not being connected
ErrorDocument 404 /index.html
</VirtualHost>
https:
<IfModule mod_ssl.c>
NameVirtualHost *:443
<VirtualHost *:443>
ServerName www.example.de
ServerAlias example.de www.example.at example.at www.example.net example.net
SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/www.example.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.de/privkey.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}$1 [R=301,L]
DocumentRoot /var/www/homepage/web
<Directory /var/www/homepage/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/example-ssl.error.log
CustomLog ${APACHE_LOG_DIR}/example-ssl.access.log combined
</VirtualHost>
</IfModule>
When I try to open http://www.example.com then it would be redirect to https://www.example.com
When I try to open http://example.com or https://example.com, then I got the error "Page not available"
What is wrong in my both conf-Files on the Webserver?
Both should redirected to the https://www.example.com url.
Please, try to avoid mod_rewrite unless there is no other option. As you can see it's complicated and will make your life harder for the simpler tasks.
A single Redirect will do to redirect everything to the SSL virtualhost:
Redirect / https://www.example.de/
Note: Redirect depends on mod_alias, so no need to RewriteEngine on or such either in the non-SSL virtualhost.
If you insist on using mod_rewrite because you have many names and need the variables:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [R,L]
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]
The first one catches those cases where host does not start with www, the second catches the rest.

Apache http-vhosts not the same as httacces?

I have a new project created in symfony and I configured httpd-vhosts to bypass all htaccess files that exist in the project's path.
<VirtualHost *:80>
ServerAdmin admin#lab.localhost
DocumentRoot "C:\wamp64\www\lab.localhost"
<Directory "C:\wamp64\www\lab.localhost">
AllowOverride None
AllowOverrideList None
Order Allow,Deny
Allow From All
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /web [L]
</IfModule>
</Directory>
<Directory "C:\wamp64\www\lab.localhost\web">
AllowOverride None
AllowOverrideList None
Order Allow,Deny
Allow From All
DirectoryIndex app.php
Options FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
</Directory>
ServerName lab.localhost
ServerAlias www.lab.localhost.com
ErrorLog "logs/lab.localhost-error.log"
CustomLog "logs/lab.localhost-access.log" common
</VirtualHost>
With this config it gives me Internal Server Error. If I set the AllowOverride and AllowOverrideList to All, it works as expected. The rewrite directves listed here are the same in my htaccess file.
Is there any difference between htaccess and httpd-vhosts?
And if not, why doesn't my config work?
Thank you

Remove index.php from Laravel 4 URL and Rewrite www to non-www

I have tried several ways to remove index.php from my URL (http://www.example.com/index.php/login), as well as redirecting www to the non-www URL.
Here is my default Apache VirtualHost file:
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/public/
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now I have added the following in to this file:
Redirect 301 / http://example.com
But this has not worked, I just get a redirect loop message. I also have the following .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
To which I've added:
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But this does not work either.
The other issue is that for all of my routes I have to include index.php, like http://example.com/index.php/test
It's as if my .htaccess file is being ignored.
The server is a DigitalOcean Droplet running Ubuntu 13.04 x64. It has PHP 5.4.9 and Apache 2.2.22.
Any help would be very much appreciated. Thanks.
Thanks to anlutro in #laravel IRC chat, he pointed out that mod_rewrite may not be enabled. Usually this is on by default in all the servers I've ever worked with so it didn't click, as I've never had to enable it but on this DigitalOcean server, it wasn't.
I used:
a2enmod rewrite to enable it and then I restarted apache. All is good.
Cheers