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

Apache Web Server Forum

htaccess 301 redirect with an exception
getting subdirectories to redirect sometimes but not always

 10:20 pm on Feb 5, 2014 (gmt 0)

Hi, my .htaccess skills have always left a lot to be desired...

I have a site where example.com/somepath needs to redirect to domain.com

But there are a couple of pages that are example.com/somepath/something.html that needs to stay as it is

I tried :

Redirect 301 /somepath http://example.com

which fixed the home page issue but took out the subdirectory of the example.com/somepath/something.html pages which made them 404s!

If anyone has a fix for this I'd be much obliged. I've searched Google for an answer.

[edited by: robcubbon at 10:39 pm (utc) on Feb 5, 2014]



 10:36 pm on Feb 5, 2014 (gmt 0)

welcome to WebmasterWorld, robcubbon!

Redirect is a mod_alias directive.
you need to use mod_rewrite directives for this.
look at using a RewriteRule preceded by a RewriteCond to exclude the exceptions from the rule.

also, please read this:
IMPORTANT: Please Use Example.com For Domain Names in Posts [webmasterworld.com]


 12:23 am on Feb 6, 2014 (gmt 0)

you need to use mod_rewrite directives for this.

... and this, in turn, means that you need to convert all your existing mod_alias directives (Redirect by that name) into mod_rewrite format. It generally isn't safe to mix modules.

But wait! If you've got a finite number of pages to redirect-- that is, "redirect A, B, C and D" rather than "redirect everything except E" --you may still be able to use mod_alias. You'll just need to change from Redirect alone, using straight text, to RedirectMatch using a Regular Expression.


 12:39 am on Feb 6, 2014 (gmt 0)

Thank you for the welcome, Phranque, I'm sure I've been here before but, apparently, not with this username and email address. I have corrected my post to go along with the guidelines however I was unable to find the solution by googling "RewriteRule preceded by a RewriteCond"

Yes, there are a very small finite number of pages to redirect, lucy24, (4, I think) however, I'm a bit unclear on what all these terms mean.

Thank you both for your input.


 4:45 am on Feb 6, 2014 (gmt 0)

Staying with mod_alias:

Redirect 301 /blahblah http://www.example.com/newpath/

A rule in this form will take any request beginning in /blahblah and send it over to /newpath instead, reappending the rest of the request:




And so on for all requests. RedirectMatch on the other hand uses Regular Expressions and captures:

RedirectMatch 301 blahblah/(one|two|three|four)\.html http://www.example.com/newpath/$1.html

(But don't quote me; I haven't used mod_alias in a couple of years.) Of course you could also write four individual rules:

Redirect 301 blahblah/one.html http://www.example.com/newpath/one.html

and repeat. When it's a grand total of four items, it really is your call.


 11:19 am on Feb 6, 2014 (gmt 0)

Thank you for your help Lucy.

I'm sorry I'm so thick but it seems that redirect 301 and redirectmatch 301 do the same thing. They apply the rules globally. I just want a rule that applies to 2 URLs and none others. How do you do that?

[edited by: robcubbon at 11:24 am (utc) on Feb 6, 2014]


 11:23 am on Feb 6, 2014 (gmt 0)

It's OK. I've used a WordPress plug-in, thank you :)

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