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
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
Redirect
matches path prefixes. The following Redirect
matches any path that’s prefix (path segment wise) is /here
and appends it to the new URI:
Redirect 301 /here http://example.com
So any request whose path begins with /here
will be redirected to http://example.com
wile appending any following path suffixes of /here
to http://example.com
.
In contrast, RewriteRule
works with regular expressions. In this case, the following RewriteRule
will match any path that contains here/
:
RewriteRule here/(.*) http://example.com/$1 [R=301,L]
Anything after here/
will be appended to the new URL.
While both directives would have the same effect when requesting URLs with paths that begin with /here
, rewriterule
will also match any request that’s path just contains here/
like /not/here/foo
.
Furthermore, you can only use additional conditions with mod_rewrite:
RewriteCond $0 !=here/stayhere.html
RewriteRule ^here(/.*)?$ http://example.com$0 [L,R=301]
If you would want to do the same with mod_alias, you will need to use RedirectMatch
with a regular expression that will match anything but /here/stayhere.html
.
The RewriteRule probably didn't work because you're saying a slightly different thing than you were in the redirect. The RewriteRule requires the address to include the word "here", followed by a slash. The redirect requires the address to have the word "here" preceded by a slash. I would go with something like this:
RewriteRule ^here/?(.*) http://www.there.com/$1 [R=301,L]
The added ?
makes the slash optional, and the ^
at the beginning makes it so that the "here" has to be at the start of the address, not anywhere inside it.
As for excluding a particular address, you'd want to do that by preceding the RewriteRule with this line:
RewriteCond %{REQUEST_URI} !(here/stayhere\.html)
That tells it to ignore the rule if they're asking for that address specifically.
精彩评论