SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help me understand .htaccess Rewrite syntax please!

    Hi,

    The scenario is that I have an SSL certificate for my domain and I want to redirect users to https://www.mydomain.com regardless of whether they include http or www in the url.

    I got it working after some Googling and trial and error but I'd appreciate it if anyone could explain to me why this works:

    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

    RewriteCond %{SERVER_PORT} !^443
    RewriteRule ^(.*)$ https://www.mydomain.co.uk/$1 [R=301,L]

    But this doesn't:

    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteCond %{SERVER_PORT} !^443
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
    "If the requested domain is not www.mydomain.com, send the request to https://www.mydomain.com"

    Code:
    RewriteCond %{SERVER_PORT} !^443
    RewriteRule ^(.*)$ https://www.mydomain.co.uk/$1 [R=301,L]
    "If the request came on port 80 (insecure) then send the request to https://www.mydomain.co.uk"

    Code:
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteCond %{SERVER_PORT} !^443
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
    "If the requested domain is not www.mydomain.com AND the request came on port 80, send the request to https://www.mydomain.com"

    A request for http://www.mydomain.com does not meet the first condition, a request for https://mydomain.com does not meet the second condition, so neither of them will be redirected.

    To get what you want, you need an [OR] between those conditions.

    Off Topic:

    Why is this in a PHP forum?

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers Dan

    I assume it makes little difference, but would I be better off leaving it as it is (with the two separate rewrite rules), or using an OR statement?

    If it's an OR statement, how would I write that?

    Apologies for posting this in the wrong place.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Billy,

    RewriteCond statements (and RewriteRule statements) are ANDed by default UNLESS an OR flag is used, i.e.,
    Code:
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC,OR]
    RewriteCond %{SERVER_PORT} !^443
    RewriteRule .? https://www.mydomain.com%{REQUEST_URI} [R=301,L]
    Note that I replaced your EVERYTHING atom in the rule's regex with the already existing {REQUEST_URI}.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers for that David - as you can probably tell I'm new to this so your help is much appreciated! I've implemented your code successfully. I guess that using the OR flag is preferable to having the two separate rewrite rules as it means that my code is simpler and faster (although the difference is no doubt negligible) - and it also means that future changes will be easier to implement.

    Thanks again

    Billy

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    No sweat, Billy. If you need more information, you might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Tags for this Thread

Bookmarks

Posting Permissions

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