Welcome to WebmasterWorld Guest from 54.226.133.245

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess 301 redirect with an exception

getting subdirectories to redirect sometimes but not always

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

New User

joined:Feb 5, 2014
posts: 4
votes: 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)

Administrator

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

joined:Aug 10, 2004
posts:10542
votes: 8


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)

Senior Member from US 

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

joined:Apr 9, 2011
posts:12696
votes: 244


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)

New User

joined:Feb 5, 2014
posts: 4
votes: 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)

Senior Member from US 

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

joined:Apr 9, 2011
posts:12696
votes: 244


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:

http://www.example.com/blahblah/
>>
http://www.example.com/newpath/

http://www.example.com/blahblah/filename.html
>>
http://www.example.com/newpath/filename.html

http://www.example.com/blahblah/otherfile.php
>>
http://www.example.com/newpath/otherfile.php


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)

New User

joined:Feb 5, 2014
posts: 4
votes: 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)

New User

joined:Feb 5, 2014
posts: 4
votes: 0


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