Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Please Tell Why to Use RewriteCond if RewriteRule is Redirecting

What is use of RewriteCond if RewriteRule is Redirecting perfectly?



6:10 am on Feb 28, 2014 (gmt 0)

5+ Year Member

Hello Mods,

I am very thankful to this forum as I have learned whole Apache Module of Rewrite from this forum.

Well, I have one question which I will think minimize some code and processing. I am asking for your suggestions mods and members as I have no idea that what is its purpose and exact usage in below code.

I have one code in my htaccess as below

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[a-z0-9-]+.php\ HTTP/
RewriteRule ^([a-z0-9-]+)\.php$ http://www.example.com/$1/ [R=301,L]

The above code is working fine and redirecting any /xyz.php page to /xyz/ but my question is RewriteCond is matching URL with pattern, if pattern matches then RewriteRule will execute and with help of [R] flag it redirects.

I want to ask that what I want is just redirection so is RewriteRule with [R] flag is not enough for it? as same pattern I am using in RewriteCond is in RewriteRule and if I remove RewriteCond line my page is redirecting perfectly.

Please give your valuable suggestions MODs.

Waiting for replies.

Best Regards


6:54 am on Feb 28, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

This situation covers when you also have another rule that matches extensionless URL requests and rewrites to internally fetch a .php file to serve the content.

In that case, the RewriteCond is necessary to redirect only those requests where the original URL request contained ".php".

The Condition matches the literal
GET /this.php HTTP/1.1
request made by the browser.

Without the Condition these requests would lead to an infinite redirect - rewrite - redirect - rewrite .... loop.

The Condition stops the redirect from happening when the file pointer is ".php" as a result of an internal rewrite.


7:34 am on Feb 28, 2014 (gmt 0)

5+ Year Member

Thanks g1smd for quick response.

I tested it and browser saying that same thing page isn't redirecting properly.

Very much thanks dear, now I understand it clearly

Best Regards


12:46 pm on Feb 28, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

RewriteRule with [R] flag

If you meant literally [R] as opposed to [R=301,L]:

[R] by default is a 302 temporary redirect. (The same applies to redirects created with mod_alias.) Normally you want a 301, so you have to say so explicitly.

Unlike some flags, [R] of any kind doesn't carry an implied [L]. So you have to say that separately.

I have never personally seen a redirect coded as R alone without [L], but they must exist.

Featured Threads

Hot Threads This Week

Hot Threads This Month