Jump to content

.htaccess issue


mjurmann

Recommended Posts

Hello. I need to redirect all https requests to http, however, I need to make one exception for a single page on the site. All of the pages redirect from https to http correctly, however, when the exception page is loaded, there is a "Redirect Loop" error displayed. Can someone please tell me where I'm going wrong? Thanks.

 

 

  # Ensure Correct Host 
  RewriteCond %{HTTP_HOST} ^domain\.com$
  RewriteRule (.*) http://www.domain.com/$1 [R=301] 

  # SSL Exception Here 
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]
  
  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^pay-invoice
  RewriteRule .? http://www.domain.com%{REQUEST_URI} [R,L]

Link to comment
Share on other sites

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]

 

 

Of course it's looping.

 

 

You're saying:

 

"If it's port 443 and the page starts with pay-invoice, redirect it to pay-invoice."  Wouldn't pay-invoice start with pay-invoice and be on port 433?

 

 

Try removing the #SSL exception block and it should work, since the Not Anywhere Else blcok should catch it.

Link to comment
Share on other sites

I set my server up to test this code and I finally figured out what is wrong. The %{REQUEST_URI} var includes the slash at the beginning, and your RewriteCond does not include that slash so it is not preventing the third RewriteRule from executing, thus causing the loop.

 

Try this code:

  # Ensure Correct Host
  RewriteCond %{HTTP_HOST} ^domain\.com$
  RewriteRule (.*) http://www.domain.com/$1 [R=301]

  # SSL Exception Here
  RewriteCond %{SERVER_PORT} !^443$
  RewriteCond %{REQUEST_URI} ^/pay-invoice
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^/pay-invoice
  RewriteRule (.*) http://www.domain.com/$1 [R,L]

 

Let me know if it works for you!

Link to comment
Share on other sites

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]

 

 

Of course it's looping.

 

 

You're saying:

 

"If it's port 443 and the page starts with pay-invoice, redirect it to pay-invoice."  Wouldn't pay-invoice start with pay-invoice and be on port 433?

 

 

Try removing the #SSL exception block and it should work, since the Not Anywhere Else blcok should catch it.

 

Thanks for your help, however, when I remove that block, mod_rewrite no longer knows to forward the http to the https version of the pay-invoice page.

 

Foxtrot - thanks for your help, but that didn't work.

Link to comment
Share on other sites

Yeah it's possible.

 

 

 

Hrmmm...

 

That's really weird....

 

RewriteEngine On


# SSL Exception Here
  RewriteCond %{SERVER_PORT} !^443$
  RewriteCond %{REQUEST_URI} ^/pay-invoice
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^/pay-invoice
  RewriteRule (.*) http://<myhost>/$1 [R,L]

 

 

Just worked fine for me x.x.

Link to comment
Share on other sites

Yeah it's possible.

 

Hrmmm...

 

That's really weird....

 

RewriteEngine On


# SSL Exception Here
  RewriteCond %{SERVER_PORT} !^443$
  RewriteCond %{REQUEST_URI} ^/pay-invoice
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^/pay-invoice
  RewriteRule (.*) http://<myhost>/$1 [R,L]

 

 

Just worked fine for me x.x.

 

I copied and pasted that exact code and substituted the domain, however, there is still an infinite loop.

Link to comment
Share on other sites

Is

 

  # Ensure Correct Host 
  RewriteCond %{HTTP_HOST} ^domain\.com$
  RewriteRule (.*) http://www.domain.com/$1 [R=301] 

  # SSL Exception Here 
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]
  
  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^pay-invoice
  RewriteRule .? http://www.domain.com%{REQUEST_URI} [R,L]

 

 

Your entire file?

Link to comment
Share on other sites

Here is what I currently have:

 

  # Ensure Correct Host 
  RewriteCond %{HTTP_HOST} ^domain\.com$
  RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

  # SSL Exception Here 
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]
  
  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^pay-invoice$
  RewriteRule .? http://www.domain.com%{REQUEST_URI} [L,R=301]
  

Link to comment
Share on other sites

RewriteCond %{REQUEST_URI} !^pay-invoice$

 

 

Change that to

RewriteCond %{REQUEST_URI} !^pay-invoice

 

 

The $ means the end.  So, that means if the request URI is not exactly equal to "pay-invoice".

 

 

I'm assuming you want if the request URI does not start with pay-invoice.

Link to comment
Share on other sites

Unfortunately, this is still giving us an infinite loop on the pay-invoice page only.

 

  # Ensure Correct Host
  RewriteCond %{HTTP_HOST} ^domain\.com$
  RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

  # SSL Exception Here
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]

  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^pay-invoice
  RewriteRule .? http://www.domain.com%{REQUEST_URI} [L,R=301]

Link to comment
Share on other sites

Hrmmm, it would seem that SERVER_PORT is never 443....

 

 

Try changing

 

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]

 

 

to

 

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice/%{SERVER_PORT} [R,L]

 

 

Temporarily so we can see what SERVER_PORT Is.

Link to comment
Share on other sites

Hrmmm, it would seem that SERVER_PORT is never 443....

 

 

Try changing

 

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]

 

 

to

 

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice/%{SERVER_PORT} [R,L]

 

 

Temporarily so we can see what SERVER_PORT Is.

 

https://www.domain.com/pay-invoice/80

Link to comment
Share on other sites

Hrmmm, it would seem that SERVER_PORT is never 443....

 

 

Try changing

 

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice [R,L]

 

 

to

 

  # SSL Exception Here

  RewriteCond %{SERVER_PORT} !^443$

  RewriteRule ^pay-invoice https://%{HTTP_HOST}/pay-invoice/%{SERVER_PORT} [R,L]

 

 

Temporarily so we can see what SERVER_PORT Is.

 

https://www.domain.com/pay-invoice/80

 

However, when I changed:

 

  # Not Anywhere Else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteRule .? http://www.domain.com%{REQUEST_URI}/%{SERVER_PORT} [L,R=301]

 

and refreshed the https://www.domain.com/pay-invoice page, it returned, http://www.domain.com/pay-invoice/443

Link to comment
Share on other sites

Your:

RewriteCond %{REQUEST_URI} ^pay-invoice$

and

RewriteCond %{REQUEST_URI} !^pay-invoice$

 

Should be:

RewriteCond %{REQUEST_URI} ^/pay-invoice$

and

RewriteCond %{REQUEST_URI} !^/pay-invoice$

 

 

respectively.

 

We're using the Drupal CMS, so unfortunately, that change does not work either.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.