Forum Moderators: phranque

Message Too Old, No Replies

Redirect and RewriteRule

         

beingk

11:00 am on Oct 27, 2009 (gmt 0)

10+ Year Member



So I'm trying to set a general redirect rule and a specific redirect, but it seems like the RewriteRule takes over the Redirect. So if I request http://example.com/folder/subfolder I end up at http://example.com/different-folder. Any way to make both of these work together?

Redirect /folder/subfolder http://example.com/some-folder [L,R=301]
RewriteRule /folder?(.*) http://example.com/different-folder [R=301,L]

Thanks!

[edited by: jdMorgan at 1:12 pm (utc) on Oct. 27, 2009]
[edit reason] example.com [/edit]

jdMorgan

1:21 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not clear what you *do* want to happen, or what the differences in the two sets of requested URLs might be -- You have to tell Apache which URLs you do and/or do not wish to be affected by each directive.

You've further complicated things by using directives from two completely-different Apache modules, mod_alias and mod_rewrite. Your syntax for the Redirect directive is incorrect, and I'd strongly suggest that you take a look at the Apache documentation for both modules.

As written, your RewriteRule directive will affect *any* URL that *contains* "/folder" or "/folde", and so will always affect the same URLs (and more) that your Redirect directive affects. I suspect that you'll be much happier with more-specific and better-anchored patterns, and attention to the required syntax for each directive.

Jim

beingk

4:42 pm on Oct 27, 2009 (gmt 0)

10+ Year Member



OK, I'll try to explain with an example:

[example-old.com...] needs to map to [example-new.com...]

but

[example-old.com...] needs to map to [example-new.com...]

and

[example-old.com...] needs to map to [example-new.com...]

So I suppose this translates to something like this?


RewriteCond %{REQUEST_URI} ^press-center(.*)
RewriteCond %{REQUEST_URI} !^press-center/press-releases/?$
RewriteRule ^$ http://example-new.com/media? [L]

Redirect /press-center/press-releases http://example-new.com/media/press? [R=301,L]

Thanks!

jdMorgan

7:15 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using mod_rewrite for both redirects and putting the most-specific rule first should solve the problem, assuming that the 'real' old and new URLs (as opposed to these examples) are non-overlapping:

RewriteRule ^press-center/press-releases/?$ http://example-new.com/media/press? [R=301,L]
#
RewriteRule ^press-center(/.*)?$ http://example-new.com/media? [R=301,L]

In order to save time, I'd suggest that if you use *any* mod_rewrite directives, you do not 'mix and match' and try to use either the Redirect or RedirectMatch directives of mod_alias; Just use mod_rewrite for all external redirects and internal rewrites.

I assume that you added the "?" to both substitution URLs in order to remove any query strings from the requested URLs, although that is *not* what would happen were you to continue to use a mod_alias "Redirect" directive. I have reproduced that feature here.

Jim

g1smd

12:14 am on Oct 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't mix "Redirect" and "RewriteRule" directives in the same .htaccess file. The order they are processed is server dependent and not dependent on the order you write them.