Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between [R=301] and [R=301,L]?

Can anyone share with me the difference between [R=301] and [R=301,L] in a 301 redirect? Which is best suited to redirect urls?

While both works perfectly fine, I do noticed that [R=301,L] changes the URL to the new URL while [R=301] only redirects the contents.

Can anyone share some insight on this please?

like image 600
bernie Avatar asked Sep 23 '13 12:09

bernie


People also ask

What does R 301 L mean?

*). The next part is also important since it does the 301 redirects for us automatically: [L, R=301]. L means this is the last rule in this run. After this rewrite, the webserver will return a result. The R=301 means that the web server returns a 301 moved permanently to the requesting browser or search engine.

What is RewriteCond %{ Http_host?

In this article I will give you a quick overview of what RewriteCond is and How it is used with RewriteRules. RewriteCond is an apache mod-rewite directive which is used for conditional URL rewriting on Apache server. We use RewriteCond along with RewriteRule%{HTTP_HOST} , %{HTTPS} etc.

What is NC in RewriteCond?

NC|nocase. Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner.

What does l mean in htaccess?

L (last - stop processing rules) Last rule: instructs the server to stop rewriting after the preceding directive is processed. N (next - continue processing rules) NC (case insensitive) NE (do not escape special URL characters in output) NS (ignore this rule if the request is a subrequest)


3 Answers

Irrespective of R=301 there L flag means Last and should be placed in rules when you want to mark end a particular rewrite rule.

As per the official doc:

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

like image 148
anubhava Avatar answered Oct 02 '22 12:10

anubhava


R makes a redirect and L forbids to apply any rules below this one. If there are no L then the rest rules will be applied to the new URL.

Official documentation says:

You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.

like image 24
AlexZam Avatar answered Oct 02 '22 13:10

AlexZam


The main difference between the flags [L] and [R=301,L] is that the R flag causes an explicit external HTTP redirect (there is one exception) while without the R flag the rule could also cause just an implicit internal redirect, depending on the actual rule and request.

The optional status code just specifies the type of the redirection response. 301 denotes a permanent redirect:

like image 35
Janak Prajapati Avatar answered Oct 02 '22 14:10

Janak Prajapati