View Full Version : Reversing Rewrite Rules?

Joseph Witchard
08-14-2010, 05:36 AM
When I rewrite a URL with mod_rewrite, I've always done something to this effect:

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:

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?

08-18-2010, 10:50 PM
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:

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:

[R=301] [R=302]

Hope that clears it up.