Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder Limey10's Avatar
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    63
    Thanks
    9
    Thanked 0 Times in 0 Posts

    The correct order of rules in htaccess?

    Hi all.

    I would like to alter my htaccess file to allow www to redirect to non-www. Which i believe is accomplished like so:

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
    RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
    I also need to ensure that only my payments page loads as https.

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]
    I have found how to make either the entire site (as above) or a folder within that site load as https, but not a single page :-(.

    My additional question is that once I have those two rules, how (and in what order) should I list them in htaccess?

    Thanks in advance for any solutions suggested
    Phil
    "I'm still smiling because they haven't found the bodies yet.....muhahaha (evil laugh)"
    GrammarProofing

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Either

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
    RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]
    or

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
    RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
    Either both will work or if one rewrite does affect the other you will soon find out which way works.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder Limey10's Avatar
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    63
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hi Felgall.

    Thanks for your reply.

    However, neither of those worked. They both gave messages that pages had moved permanently, and even when pages id load, they still contained the www prefix.

    Regards, Phil
    "I'm still smiling because they haven't found the bodies yet.....muhahaha (evil laugh)"
    GrammarProofing

  • #4
    New Coder Limey10's Avatar
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    63
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Just a quick update.

    I have got to this point where I can now redirect all pages to non-www and have all pages redirect to HTTPS.

    However, I am still unable to redirect only a single page to HTTPS instead of all of them.

    If anyone has the answer, please, please, please help.

    This is my code so far:
    Code:
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^sample\.com$ [NC]
    RewriteRule .* http://sample.com%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    Thanks, Phil
    "I'm still smiling because they haven't found the bodies yet.....muhahaha (evil laugh)"
    GrammarProofing

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    Options +FollowSymlinks
    
    RewriteEngine On
    
    # Make sure the login.html page is HTTPS (and www. to non-www.).
    RewriteCond %{HTTPS} off
    RewriteRule ^login\.html$ https://example.com%{REQUEST_URI} [L,R=301]
    
    # www. to non-www.
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
    RewriteRule .* http://example.com%{REQUEST_URI} [L,R=301]
    You could also add
    Code:
    # HTTP for all pages except login.html (and www. to non-www.).
    RewriteCond %{HTTPS} on
    RewriteRule !^login\.html$ http://example.com%{REQUEST_URI} [L,R=301]
    between those rules to send all HTTPS not login.html requests to HTTP.

  • Users who have thanked schleppel for this post:

    Limey10 (07-28-2014)

  • #6
    New Coder Limey10's Avatar
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    63
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hi Schleppel

    Thank you very much for your kind reply.

    I have to admit, I had a similar answer from another forum about 10 minutes before yours, and it seems the order of the rules is most important. I did try your code, but it has to be the www redirect before the http redirect. As follows:

    Code:
    Options +FollowSymlinks
     RewriteEngine On
     RewriteBase /
     RewriteCond %{HTTP_HOST} !^sample\.com$ [NC]
     RewriteRule .* http://sample.com%{REQUEST_URI} [L,R=301]
    
     RewriteCond %{HTTPS} off
     RewriteRule ^one/single/page\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Thanks anyway

    Phil
    "I'm still smiling because they haven't found the bodies yet.....muhahaha (evil laugh)"
    GrammarProofing


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •