homepage Welcome to WebmasterWorld Guest from 54.166.116.36
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 Redirect for some pages and another one for all other pages
lafronde



 
Msg#: 4579052 posted 3:16 pm on May 29, 2013 (gmt 0)

Hello,

I would appreciate any help trying to figure out how to set a a rule for a few specific pages and a general one for all the others.

My latest attempt failed (after a few hours trying to figure out)

Redirect 301 /oldpage1 http://example.com/whatever/
Redirect 301 /oldpage2 http://example.com/whatever/thispage.php

RewriteEngine on
RewriteRule (.*) http://example.com [L,R=301]

Thank you in advance.

[edited by: phranque at 12:26 pm (utc) on May 30, 2013]
[edit reason] use example.com please [/edit]

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4579052 posted 7:14 pm on May 29, 2013 (gmt 0)

Someone will come along shortly and change your URLs to "example.com" (see Forums charter and also format of post when you don't exemplify).

When you say "failed", you know what you mean but we don't. It can be anything from "crashed the server" to "had unintended effects" to "had no effect at all".

Never never never combine mod_alias (Redirect by that name) with mod_rewrite. You have no control over what order they'll execute in. On shared hosting mod_rewrite will generally happen first. But it isn't safe to make any assumptions, so use only one or the other.

In this case, simply configuring all the redirects as RewriteRules may be enough to solve the problem. Or, if it turns out that all rules will work in mod_alias, use that.

lafronde



 
Msg#: 4579052 posted 8:04 pm on May 29, 2013 (gmt 0)

Hi Lucy,

Sorry for the domain name, thank you for the information.

To be more precise, 'failing' means that all redirect are heading to "example.com", the two previous specific rules are not taken into account, but no crash. I understand that I must not mix mod_alias and mod_rewrite. I admit my weekness why rewrite rules, so complex. :-)

Dideved



 
Msg#: 4579052 posted 8:46 pm on May 29, 2013 (gmt 0)

...the two previous specific rules are not taken into account, but no crash.


This is almost certainly due to the execution order that Lucy described. Admittedly this part of Apache's behavior isn't at all obvious. Even though your code lists the two "oldpage" redirects first, they will nonetheless execute _after_ the rewrite redirect.

I also agree with Lucy's proposed solution. Due to this non-obvious execution order, it's probably better to use _only_ Redirect or _only_ RewriteRule. That way things will execute in an obvious top-down order.

lafronde



 
Msg#: 4579052 posted 8:58 pm on May 29, 2013 (gmt 0)

Do you have a sample that I can test using either method? No worries with results, I can backup easily if there are any issues. By the way, sorry with the last sentence of my last posting, please read "I admit my weakness with rewrite rules"... Thank you.

Dideved



 
Msg#: 4579052 posted 9:13 pm on May 29, 2013 (gmt 0)

For example, by changing:

RewriteRule (.*) http://newdomain [L,R=301]

to:

Redirect 301 / http://newdomain/
lafronde



 
Msg#: 4579052 posted 9:31 pm on May 29, 2013 (gmt 0)

Yes, only issue is that it's carrying the old path which leads to a 404 on new domain while it should go straight to newdomain root. The two only exceptions are the 2 oldpages redirect to a specific newdomain page.

Dideved



 
Msg#: 4579052 posted 9:52 pm on May 29, 2013 (gmt 0)

OK. In that case, perhaps...

RedirectMatch 301 (.*) http://newdomain/

Note that it's also an option to instead convert the other "oldpage" redirects to rewrite redirects.

lafronde



 
Msg#: 4579052 posted 10:04 pm on May 29, 2013 (gmt 0)

I could make it work using: RedirectMatch 301 ^/ [newdomain...] Does it look ok to you? It's working fine for the oldpages and .* to newdomain as well.

Dideved



 
Msg#: 4579052 posted 11:41 pm on May 29, 2013 (gmt 0)

Yup, that's totally fine. Perhaps even slightly better than my last post, since you don't actually need to capture the path, you just need a positive match.

lafronde



 
Msg#: 4579052 posted 8:07 am on May 30, 2013 (gmt 0)

Perfect, I'm proud! :-) Thank you very much to you Dideved and Lucy24.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4579052 posted 12:27 pm on May 30, 2013 (gmt 0)

welcome to WebmasterWorld, lafronde!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved