0

I'm going crazy with a Rewrite Rule on Apache.

Basically what I want to achieve is to rewrite any url like:

http[s]://www.example.com/something 

to

https://www.example.com 

I have a VHost on apache like the following:

<VirtualHost *:80> ServerName example.com ServerAlias example DocumentRoot /var/www/html/example_courtesy ServerAdmin [email protected] RedirectMatch 404 /\.git RedirectMatch 404 /\.svn <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/html/example_courtesy> Options Indexes FollowSymLinks MultiViews AllowOverride None Require all granted DirectoryIndex index.php indice.htm </Directory> RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R] </VirtualHost> <IfModule mod_ssl.c> <VirtualHost *:443> ServerName example.com ServerAlias example DocumentRoot /var/www/html/example_courtesy ServerAdmin [email protected] RedirectMatch 404 /\.git RedirectMatch 404 /\.svn 

[...]

I tried deleting the [L] from the first rule and adding a rewrite rule like the following into *:443 VirtualHost:

 RewriteEngine on RewriteCond %{SERVER_PORT} !^80$ RewriteRule ^.*$ https://%{SERVER_NAME} [L,R] 

What I receive is a rewrite loop, Firefox tells me "The page isn't redirecting properly".

I did many other tries with rewrite rules but no luck.

I only achieved to rewrite a specific URL like https://www.example.com/specific-path to https://www.example.com with a RedirectMatch, but this is not what I definitely want.

Any suggestions?

I searched here for a similar question but I didn't find a solution to my specific problem.

1 Answer 1

2

Your RewriteCond for the *.443 section has an obvious issue with it. HTTPS (typically) runs on port 443 (as the VirtualHost configuration shows) but your rewrite condition says 'If the server port is not 80, redirect to https://...'.

So, hit port 443, ask for content, get told to go to 443 because you're not on 80. That's a loop. RewriteCond %{SERVER_PORT} !^443$ should work.

Personally, I would rather use RewriteCond %{HTTPS} !=on.

3
  • It works if I use RewriteCond %{HTTPS} !=on but only for HTTP requests. But if I want to redirect even the HTTP request how can I do? I would like to have any http or https request like http[s]://www.example.com/something redirected to https ://www.example.com/, I hope I was clear, thanks! Commented Jun 8, 2016 at 16:39
  • There was a typo, I would like to redirect HTTPS requests too. Maybe should I use mod_redirect instead of mod_rewrite? Commented Jun 9, 2016 at 6:17
  • Your problem is really a simple one if you follow it through logically. You haven't yet defined (in your conf nor your question) what you don't want to be redirected from http to https. Define it in your head, then define it in your conf. Apache is only going to do what you explicitly tell it to. Commented Jun 9, 2016 at 6:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.