Last Updated on April 21, 2021 by Amit
Both R and R=301 flags are used to redirect URLs using RewriteRule directive. Some new mod-rewrite users often get confused with whether they should use R or R=301 in their RewriteRule.
In this post I will explain both flags and when to use these flags in your Rule.
R flag in RewriteRule
R represents a temporary redirection. R is same as R=302 so you can use one of these two forms of the flag to redirect your URL with temporary redirect status code. Both R and R=302 issue a 302 redirect status to clients. 302 is a temporary redirect.
R=301 flag in RewriteRules
R=301 is a permanent redirection flag. It uses 301 status to redirect the URL from one location to the other.
When to use R
You should this flag for testing purposes only. Since it’s a 302 temporary redirect , if you use this on a real site URL then you can get into trouble of loosing your search engine rank. As search engines do not index temporary redirects.
The benefit of using temporary redirect flag while testing RewriteRules is that it isn’t cached by your web browser. You can test your RewriteRule freely without worrying about browser cache.
You can also use R flag when you want to redirect your site URL to a different URL or path for some time. This redirection won’t be noticed by search engines.
When to use R=301 vs R in RewriteRules
R=301 is a permanent redirect flag. It is cached by your web browser and indexed by a search engine. You can’t use it while testing your RewriteRule code as it’s cacheable by browsers. Once you have verified your rules are working you can then change your R to R=301 . It’s recommended to do so unless you are really doing a temporary redirection.
A 301 redirect tells search engines that the URL has changed . This tells search engines to update their databases with a new URL.
In general , if you are using a RewriteRule just to test it on your server then you must avoid using R=301 as it can be cached and remains in the browser memory until you clear it.