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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts

    Reversing Rewrite Rules?

    When I rewrite a URL with mod_rewrite, I've always done something to this effect:

    Code:
    RewriteRule ^show_news/([0-9]+)$ show_news.php?post_id=$1
    However, I found a script online to force the user to connect to the website via HTTPS, and it was something like:

    Code:
    RewriteRule ^(.*)$ https://example.com/$1 [R,L]
    As you can see, the last bit is reversed compared to the first bit. What I mean is in the first code ends with the URL that's being rewritten. The last bit of code ends with the URL that's being created as the new URL. Is that because of the R flag that comes at the end?

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    I don't see it as being reversed. Essentially, it points any matching requests (The first string) to the specified https location (Second string)

    The [R] flag stands for redirect, so it forces the browser to load the new location (Including the change in the address bar). Quite often used by popular websites for adding "www." to the front of their domain. Like so:

    Code:
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com$1 [R,L]
    You can also add the redirect type (301 or 302) to the flag like so:

    Code:
    [R=301] [R=302]
    Hope that clears it up.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?


  •  

    Posting Permissions

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