Forum Moderators: phranque

Message Too Old, No Replies

redirect both HTTP and HTTPS to single page

         

nychris311

8:47 pm on Jun 13, 2012 (gmt 0)

10+ Year Member



After Googling, this seems to be the place to come to to get Apache answers. I'm able to redirect all HTTP or HTTPS requests to a single page, but cannot have both configured at once or I get redirect loop errors. This code works if I put it in the default HTTP section or the <VirtualHost _default_:443> section, but not both at the same time.


RewriteEngine on
RewriteRule ^(.*) http://www.example.com/decom.html [L,R=301]


How can I accomplish this for any requests regardless if it's HTTP or HTTPS?

Thanks!

g1smd

8:58 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's easy to do by examining more than the requested path.

Look at the SERVER_PORT and REQUEST_URI each in separate preceding RewriteConds.

You'll need a ruleset for http requests (SERVER_PORT is not 443) and a ruleset for https requests (SERVER_PORT is 443).

You'll need to exclude http requests for the redirected-to URL from being redirected. This RewriteCond looking at REQUEST_URI will prevent a redirect loop.

nychris311

9:27 pm on Jun 13, 2012 (gmt 0)

10+ Year Member



It worked! Thanks a lot for your help!

Default port 80 section:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !decom.html
RewriteRule ^(.*) [example...] [L,R=301]

Port 443 section:

RewriteEngine on
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !decom.html
RewriteRule ^(.*) [example...] [L,R=301]

nychris311

9:32 pm on Jun 13, 2012 (gmt 0)

10+ Year Member



FYI, it looks like the site changed my code above. I'll try it again:

Default port 80 section:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !decom.html
RewriteRule ^(.*) http://example.com/decom.html [L,R=301]

Port 443 section:

RewriteEngine on
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !decom.html
RewriteRule ^(.*) http://example.com/decom.html [L,R=301]

g1smd

9:33 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep, that's pretty close.

%{HTTPS}
isn't always available.
%{SERVER_PORT}
might be more reliable.

Escape the literal periods in the RegEx patterns.

RewriteEngine On
is needed only once.

The rule will run slightly faster if you change the
!decom.html
pattern to
!^/decom\.html$

nychris311

9:46 pm on Jun 13, 2012 (gmt 0)

10+ Year Member



Excellent, thanks again. Here's my code below. FYI, removing the "RewriteEngine on" from the 443 Virtual host section stopped the SSL rewrites so I had to put it back in.


In httpd.conf main section:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} !^/decom\.html$
RewriteRule ^(.*) http://example.com/decom.html [L,R=301]

In the <VirtualHost _default_:443> section of ssl.conf...

RewriteEngine on
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/decom\.html$
RewriteRule ^(.*) http://example.com/decom.html [L,R=301]

g1smd

9:53 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm so used to people scattering the
RewriteEngine On
directive multiple times in every file, it's automatic to state that it's only needed once.

In this case, as you have the two rulesets in separate containers, you do need it once per container.


I'd swap out
%{SERVER_PORT} ^80$
for
%{SERVER_PORT} !^443$
just in case someone comes to your http site on port 8080 or something.


The last thing I missed is that
^(.*)
can be simplified to
.*
here.

[edited by: g1smd at 9:59 pm (utc) on Jun 13, 2012]

lucy24

9:54 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Escape the literal periods in the RegEx patterns.

Speaking of patterns:

Since you're not doing anything with the requested URL, you don't need the (.*) parentheses. And since there are no restrictions on what it can be-- at least not in the Rule-- you also don't need the ^ opening anchor. All you need is

RewriteRule . http:// et cetera for "any request whatsoever".

removing the "RewriteEngine on" from the 443 Virtual host section stopped the SSL rewrites so I had to put it back in.

Makes sense. It's analogous to including the statement separately in each htaccess; it's one of the few things in Apache that aren't inherited.

g1smd

10:02 pm on Jun 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@lucy The pattern needs to be
.*
here.

If you use just
.
then root requests would not be redirected.



I'm assuming this is for posting a notice on a decomissioned website.

There's another way to do it. Remove all files except for the one with the notice and then use:

ErrorDocument 404 /decom.html

lucy24

5:28 am on Jun 14, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Or keep the files, block everyone but yourself, and do the same thing with a 403 ;)