Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite with Conditions UGG!

Holy Grail of Rewrites?

         

produceresults

7:51 am on Jan 6, 2011 (gmt 0)

10+ Year Member



Ok... I have been reading and banging my head against a wall for 2 days. I need assistance. Here is what I am trying to accomplish:

We want to redirect customers to the new site's homepage no matter what page they are on with a few exceptions.

Exception 1: We need to make sure the the /admin/ directory and ALL of its contents do NOT redirect

Exception 2: There are 4 pages (/page.html, page2.html, etc) that we want to redirect to specific subpages and not to the homepage.

I have 2/3rds of the equation but can't seem to get this all together. Here is what I have:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^attitudes-gratitude-give-receive-every-your-life-p-364\.html$ http://beta.walkthetalk.com/attitudes-of-gratitude-how-to-give-and-receive-joy-every-day-of-your-life.html [R=301]
RewriteRule ^tuesday-morning-coaching-p-566\.html$ http://beta.walkthetalk.com/tuesday-morning-coaching.html [R=301]
RewriteRule ^change-good…you-first-p-260\.html$ http://beta.walkthetalk.com/change-is-goodyou-go-first.html [R=301]
RewriteRule ^walk-talk-development-p-453\.html$ http://beta.walkthetalk.com/walk-the-talk-development-kit.html [R=301]
RedirectMatch permanent /.* http://beta.walkthetalk.com/


The part that is missing is the condition !^admin/$

Can anyone help?

Thanks,

Scott

jdMorgan

10:31 pm on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do not mix RewriteRule directives with Redirect and RedirectMatch directives. The former is processed my mod_rewrite, while the latter two are processed by mod_alias. The because config code is processed by each module in turn, and not in strict line-by-line order, the order of 'execution' of these directives will vary depending on the main server configuration -- which could change at any time without your knowledge. This can (and does) often lead to unexpected behavior, because the order of execution of mod_rewrite and mod_alias directives can suddenly change...

If you want to make a RewriteRule conditional based upon something in addition to the single RewriteRule pattern, there is a directive for that called "RewriteCond" -- something that can easily be found in the mod_rewrite documentation at apache.org... :)

And note that patterns for RewriteRules and RewriteConds alike can be negated using the "!" NOT operator.

Hopefully, that will get you well-started.

Jim

produceresults

3:23 pm on Jan 7, 2011 (gmt 0)

10+ Year Member



I wanted to update this post with my final result. I figured out how to accomplish this task so to anyone who wants to do the following here is the basic code.

Redirect every page on one domain to new domain's homepage and excluding 1 or more directories as well as having a few pages redirect to specific subpages:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^old_page1\.html$ http://www.newdomain.com/old_page1.html [L]
RewriteRule ^old_page2\.html$ http://www.newdomain.com/old_page2.html [L]
RewriteRule ^old_page3\.html$ http://www.newdomain.com/old_page3.html [L]
RewriteRule ^old_page4\.html$ http://www.newdomain.com/old_page4.html [L]
RewriteRule ^admin - [L]
RewriteRule ^blog - [L]
RewriteRule ^images - [L]
RewriteRule ^ http://www.newdomain.com/ [R=301]

jdMorgan

6:23 pm on Jan 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Demonstrating the previously-recommended method, along with some syntax and efficiency tweaks, one of which will prevent damage to your pages' search rankings:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^old_page1\.html$ http://www.newdomain.com/old_page1.html [R=301,L]
RewriteRule ^old_page2\.html$ http://www.newdomain.com/old_page2.html [R=301,L]
RewriteRule ^old_page3\.html$ http://www.newdomain.com/old_page3.html [R=301,L]
RewriteRule ^old_page4\.html$ http://www.newdomain.com/old_page4.html [R=301,L]
#
RewriteCond $1 !^(admin|blog|images)
RewriteRule ^ http://www.newdomain.com/ [R=301,L]

Note that the final rule can then be further optimized to:

RewriteRule !^(admin|blog|images) http://www.newdomain.com/ [R=301,L]

Note that due to SEO considerations, I would recommend that you *not* redirect more than your top dozen pages to the homepage on the new domain. This may raise a big red flag in the search engines' "quality-judging" routines, because it often occurs when a site changes owners or dramatically changes its subject. When the site changes so much that large numbers of URLs cannot be properly redirected to similar relevant content in the new URL-space, then search engines consider that you have no right to claim that all of this content has "Moved Permanently" to a single home page -- There is simply no way that that home page can or should be relevant to the searches which used to land on the individual pages.

So, caution and cold, calculated objectivity are advised here. You could well destroy the new site before it even sees any traffic.

For URLs which cannot be 301-redirected to an *exact* replacement page, the proper response is a 410-Gone displaying a page that explains that the old pages are gone and offering text links to the new site's home page, its HTML site map page, its search page, and perhaps to it s major categories page.

Anyway, just be warned...

Jim