Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between RewriteRule and redirect 301

I was using something like this...

RewriteRule here/(.*) http://www.there.com/$1 [R=301,L] 

but couldn't get it to work

So I used this...

redirect 301 /here http://www.there.com 

and it worked fine.

Can someone explain the difference? Do you need both?

Also... how do I exclude paths from the redirect?

Like... redirect 301 all...

redirect 301 /here http://www.there.com 

but

/here/stayhere.html

Thanks

like image 375
gravityboy Avatar asked Nov 29 '10 20:11

gravityboy


People also ask

What is Rewriterule?

L|last. 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.

What is L R 301?

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 rewrite cond in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


1 Answers

RewriteRule is handled by Apache's mod_rewrite, while Redirect is handled by mod_alias. No, you don't need both.

Your RewriteRule (which uses regex) will not match /here (but will match such paths as /here/foo.html) because it is looking for a slash immediately after. You can make that optional by using a question mark:

RewriteRule ^here(/?.*) http://www.there.com$1 [R=301,L]

Now that will have the same effect as your Redirect. RewriteCond can be added to exclude certain paths:

RewriteCond $0 !/here/stayhere\.html

Note that some servers do not have mod_rewrite turned on by default. If adding RewriteEngine on to your configuration does not fix the problem and you cannot switch mod_rewrite on, at least mod_alias provides the RedirectMatch directive, which may be good enough:

RedirectMatch 301 ^/here(?!/stayhere\.html)(/?.*) http://www.there.com$1
like image 83
PleaseStand Avatar answered Sep 23 '22 12:09

PleaseStand