Advanced 301 Redirect

I want to stop a redirect on one page of a website. Currently it has the 301 redirect that redirects the entire domain. What code would I use if I wanted to stop one page? For example, have sitepoint.com and everything in it redirect to webdesign.com. Now you want to get sitepoint.com/badgers NOT to redirect, but you want everything else to remain the same. How would I do that please?

The current 301 in implemented how, php, htaccess?

Hi metho.

It is currently being redirected with htaccess. Like this: redirect 301 / http://sitepoint.com

To EXCLUDE a web page, you’ll need to use mod_rewrite instead of mod_alias:

RewriteEngine on RewriteCond %{REQUEST_URI} !^badgers$ RewriteRule .? http://webdesign.com [L]

You might benefit from reading the mod_rewrite tutorial at http://dk.co.nz/seo as it contains explanations and sample code (it was also the basis for sticky threads and a SitePoint article). It’s helped may members and should help you, too.

Regards,

DK

1 Like

Thank you, dklynn. I will read the tutorial. Can I just ask you quickly… Do I keep what I have currently in the .htaccess file, and then add what you have below it? Or do I delete the 301 redirect and replace it with your code? Maybe it’s in the tutorial.

Hi eLe!

As above, you need to use a different module (mod_rewrite instead of mod_alias) so you’ll have to DELETE the (Redirect) code you have. Aside from that (Redirect) code causing your problem, mod_alias is part of the Apache core so it will take precedence and redirect everything before mod_alias gets a chance to exclude badgers from its redirection.

Regards,

DK

1 Like

Thanks again, dkylnn. I just have one more question if you will. I did visit the link you sent me, but I was unclear. What if I want two different pages not to redirect? Below I added “groundhogs” to your existing code. Is this how I’d do it?

RewriteEngine on
RewriteCond %{REQUEST_URI} !^badgers$ !^groundhogs$
RewriteRule .? http://webdesign.com [L]

Also, what if the page isn’t in a folder? Ex: webdesign.com/badgers.html instead of webdesign/badgers/underground.html?

Hi eLe!

No problem - my philosophy is to simply “share the knowledge.”

Please advise what was unclear so I can improve the tutorial.

Your use of the RewriteCond regex is wrong (no offense, of course). The RewriteCond statement’s format is
RewriteCond{space}{variable_with_regex}{space}{regex_to_match}[space}{optional_flags}.

There are two ways to change that to be correct:
Simple:

... RewriteCond %{REQUEST_URI} !^badgers$ [AND] RewriteCond %{REQUEST_URI} !^groundhogs$ ...
OR:

... RewriteCond %{REQUEST_URI} !^(badgers|groundhogs)$ ...
Just remember that you do not want to redirect if either badgers or groundhogs are matched, therefore, if badgers is NOT matched AND groundhogs is NOT matched, then execute the RewriteRule.

“Unfortunately,” you can simplify this further to:

[code]RewriteEngine on
RewriteRule !^(badgers|groundhogs)$ http://webdesign.com [L]

[R=301,L] if you are making a permanent redirection.[/code]

[quote]Also, what if the page isn’t in a folder? Ex: webdesign.com/badgers.html instead of webdesign/badgers/underground.html?
[/quote]
Simply delete the $ (end of string anchor) from the badgers part of the regex.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.