homepage Welcome to WebmasterWorld Guest from 23.20.44.136
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Please Tell Why to Use RewriteCond if RewriteRule is Redirecting
What is use of RewriteCond if RewriteRule is Redirecting perfectly?
sweetguyzzz




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

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

 

g1smd




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

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.

sweetguyzzz




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

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

lucy24




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved