VirtualHost configuration (rewrite) not working when using SSL - apache

I am using the following directives to configure my VirtualHost in Plesk:
[vhost.conf]
ServerName www.mydomain.com
DocumentRoot /var/www/vhosts/mydomain.com/httpdocs
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !^.+\.(css)
RewriteCond %{REQUEST_URI} !^.+js
RewriteRule ^(.+)$ /index.php/$1/
<Directory /var/www/vhosts/mydomain.com/httpdocs>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
php_admin_flag safe_mode Off
</Directory>
This worked fine in every case, until i added an SSL certificate and accessed the server through https. Now the rewrites were no longer working, when calling the site through SSL.
So i figured that the configuration loaded for the other protocol (https) must differ from the one that is loaded in the case of http.
I then found out that Plesk uses two separate config files for both protocols. I copied vhost.conf to vhost_ssl.conf. Now the site loaded fine over https and the rewrites were working fine.
But now the rewrites were no longer working when accessing the site through http.
Seems like the cat is biting its tail, i am running in circles and out of options.
Unfortunately i lack the experience in configuring apache. I do assume that somehow my two sets of rules are causing a problem, but after all they are included into two different <VirtualHost> directives.
Perhaps someone knows what is going on here and how to fix it?

I can't tell you how to fix it but I can tell you how to start.
Look carefully at the access and error logs. Read about this here: https://httpd.apache.org/docs/2.2/logs.html
There are a bunch of tools to help you with this debugging described there and in linked pages.

Related

Apache's RequestHeader is ineffective and effect is not seen in .htaccess

This is an apache question.
I set a header in my config files with RequestHeader. (local apache install)
I can see that works as per the output of a custom php script to dump headers: the header is there.
However a rule based on that header being present is not fulfilled in .htaccess.
The same .htaccess file works as expected on another server.
My added request header doesn't seem to be visible in .htaccess.
Any idea?
Apache conf:
RequestHeader set X-Forwarded-Proto "https"
.htaccess:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
That loops forever. But it works perfectly on a config behind an AWS load-balancer.
Thanks for helping agent420.
I finally found the solution... It takes either a clear understanding of Apache processing rules or some luck (latter in my case).
The working directive is:
RequestHeader set X-Forwarded-Proto "https" early
High, unsubtle, massive emphasis on the word 'early'.
That's all it took...
Hope that comes of some use to others.
Teebo
Do other rules in .htaccess work on this server? Because if they do not then it may be due to a configuration in Apache Config file (httpd.conf or apache2.conf depending on your distro)
Edit this file. Look for your website's directory...something like:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change None toAll for the AllowOverride option. Restart the Apache service.
AllowOverride directive is used to allow the use of .htaccess within the web server to allow overriding of the Apache config on a per directory basis.
See this doc for details

.htacces files doesnt work despite of AllowOverride All

I dont know why but my simple .htaccess file doesnt work on my server.
/var/www/html/.htaccess
RewriteEngine On
#RewriteCond %{HTTP_HOST} ^/$ [R=301,L]
#RewriteRule ^(.*)$ htt://www.onet.pl/$1 [L]
RewriteRule ^/lol$ /dev/public
I tried to make a test with either redirection of all traffic to another server or simple redirection myaddress/lol to myadress/dev/public where i have index.html file.
Modul REWRITE is already enabled
#a2enmod rewrite
Module rewrite already enabled
And this is my /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
When i write rubbish in /var/www/html/.htaccess and open servers main address then it throws error message and in apache logs is see some error about wrong command in .htaccess file.
But neither of redirections placed in .htaccess works. Second redirection does not redirects from adress/lol to address/dev/public - browser just throws that there is no /dev site.
I have root access, server is Debian 8 with apache 2.4.10. What can i do?
The simple issue us that you are trying to use an absolute path with a rewrite rule inside a dynamic configuration file. It is clearly documented that in this case you need a relative path due to the nature of how those stupid dynamic configuration files work (.htaccess).
Take a look at the version using a relative path:
RewriteEngine On
RewriteRule ^lol$ /dev/public
A more intelligent way to handle this discrepancy is to use a pattern that will work in both case, in dynamic configuration files and in the real host configurations:
RewriteEngine On
RewriteRule ^/?lol$ /dev/public
But the best idea would be not to use such dynamic configuration files at all but the http servers host configuration instead. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Here is the official documentation (which you definitely want to read), take a look at the section labelled "Per-directory Rewrites":
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Apache 2.4 Whitelist Cloudflare only

I've been trying to get this to work for Apache 2.4
I manually installed Apache 2.4, PHP 5.6, MySql, and MySqli. (I haven't had a problem with mysql since I've only been using MySqli)
I have tried to white list only cloudflare to access my website, but I haven't been having any luck and since their IP list doesn't give me absolutely everything I need, I can't white list them.
I know that I shouldn't white list only cloudflare, but I accidentally shared the IP to someone and they spread it around, and I don't want them using the ip directly, I want them to use cloudflare.
I've tried doing this to white list cloudflare but it hasn't been working:
<Directory "C:/Apache24/htdocs/test">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all denied
Allow from 103.21.*.*
Allow from 103.31.*.*
Allow from 103.22.*.*
Allow from 104.16.*.*
Allow from 108.162.*.*
Allow from 131.0.*.*
Allow from 141.101.*.*
Allow from 162.158.*.*
Allow from 172.64.*.*
Allow from 173.245.*.*
Allow from 188.114.*.*
Allow from 190.93.*.*
Allow from 197.234.*.*
Allow from 198.41.*.*
Allow from 199.27.*.*
</Directory>
I havn't fully understood the jump from 2.2 to 2.4 yet. Could anybody help me out?
If you install the Mod_Cloudflare Apache extension, you can simply add the following to your Apache configuration and all non-Cloudflare traffic will be forbidden:
DenyAllButCloudFlare
Further reading:
Logging Real Visitor IP Addresses: mod_cloudflare for Apache httpd
Though I am not 100% sure this answer is appropriate to your question but I will share it in case some other people want different solution.
Cloudflare pass a real ip in HTTP_CF_CONNECTING_IP. So we can setup Rewrite(using mod_rewrite). So we don't need to whitelist cloudflare's ips, accept all ip and check the HTTP_CF_CONNECTING_IP. If there is no match, we deny. So the configuration in wordpress conf is as following. The example shows any real ip not starting with 17.133.218 is denied access.
<Directory /var/www/wordpress>
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP:CF-CONNECTING-IP} !^17\.133\.218\.
RewriteRule $ /maintenance [R=302,L]
</IfModule>
</Directory>

How can I get .htaccess to be seen when I make my site live

I have a .htaccess file which (among other things) creates pretty urls. Here's a sample of part of the file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
...
# Pretty urls
RewriteRule ^home(\d+)*$ ./index.html
RewriteRule ^contact(\d+)*$ ./contact.html
RewriteRule ^enquire(\d+)*$ ./enquire.html
RewriteRule ^terms(\d+)*$ ./terms.html
RewriteRule ^about(\d+)*$ ./about.html
RewriteRule ^owners(\d+)*$ ./owners.html
I am currently moving the site to new hosting on digitalocean. When I test the site out by browsing the IP e.g.
111.222.333.444/html_docs/contact
the .htaccess file is working perfectly. Rewrite rules are on and the site properly rewrites the url and serves (in this case, contact.html) to the browser.
When I change the nameservers and browse the site by domain after propogation e.g.
mydomain.com/contact
the .htacces file fails to do the redirect, at least, it seems like it's not being seen at all. This is weird to me since it works when I'm browsing by ip. I purposely put some rubbish into the .htaccess to test if it was being picked up. Again, if I browse via the ip I get a 500 which I would expect, but when browsing by domain name I do not get an error but the rewrites don't work.
I'm banging my head at this stage and can't figure it out. Any help appreciated,
many thanks,
Wittner
Ok, this was less of a lack of knowledge about how .htaccess works and more to do with not configuring my vhosts settings properly.
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin me#mydomain.com
DocumentRoot "/var/www/mydomain.com/html_docs/"
<Directory "/var/www/mydomain.com/html_docs/"> <-- Problem was here
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Turns out that in the tag I had left out the path to my domain root. So when I directly browsed the site via the IP the system was able to figure it out, but no while browsing. All my .htaccess stuff is now working. Thanks to all who looked and anyone who replied. Live and learn I guess :-)
cheers,
Wittner

Redirect https to http on ispconfig+apache

I know that question has already been asked, but for some reason, no matter how I try, redirect from https to http just doesn't work. I tried my default approach to redirect (always worked perfectly fine with http to https, thought it would go that smooth the other way as well) and a few solutions from here, but no effect. For now, this is the last method I've tried:
In Apache sites-available directory, I modified relevant .vhost entry, so the changed part now looks like this:
<Directory /var/www/SOMEWEBSITE/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
<Directory /var/www/clients/client1/web1/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
Still, connecting via https://SOMEWEBSITE gives me standard apache blank site
(It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.)
I tried inserting the rewrite rules directly in VirtualHost block and modyfing site's .htaccess file (same effect as above), also tried to create VirtualHost listening on port 443 and redirecting traffic to http, but this approach gave me an SSL error. Is there any other way or something I might be doing wrong? Or is that some ISPConfig issue I am not aware of?
You could try this solution, here on stackoverflow. It is not done within ISPConfig, but I do not think there is a way to do this from within the ISPConfig GUI anyway.