1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

301 redirect for an entire directory

Discussion in 'Apache' started by al2six, Jul 16, 2004.

  1. #1
    lets say i renamed a folder. does anyone know how i can write a 301 redirect in .htaccess to redirect every file in that folder to the new folder?
    thanks
    SEMrush
     
    al2six, Jul 16, 2004 IP
    SEMrush
  2. Foxy

    Foxy Chief Natural Foodie

    Messages:
    1,614
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Foxy, Jul 16, 2004 IP
  3. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #3
    Will.Spencer, Jul 16, 2004 IP
  4. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #4
    That article states:
    This is factually incorrect, which leads me to believe that the author never tested his suggestions. :eek:
     
    Will.Spencer, Jul 16, 2004 IP
  5. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here is the Apache documentation on mod_rewrite, which should help considerably in understanding how to do redirects properly.

    (It helps a lot if you are familiar with "regular expressions"--if you are not, Google up some tutorial on that before getting into the mod_rewrite docs.)
     
    Owlcroft, Jul 17, 2004 IP
  6. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #6
    Of course, you don't need to learn about mod_rewrite to do a 301 redirect in .htaccess. :)
     
    Will.Spencer, Jul 17, 2004 IP
  7. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #7
    True; what you can often get by knowing about is mod_alias, the Apache documentation for which is at http://httpd.apache.org/docs/mod/mod_alias.html, where they note that A more powerful and flexible set of directives for manipulating URLs is contained in the mod_rewrite module.

    It's simply the classic power-vs-simplicity tradeoff thing. The mod_alias Redirect directive can adequately handle many straightforward redirects; its RedirectMatch can handle slightly more complex arrangements; but mod_rewrite is the heavy artillery, for when complex redirects are needed. (And "complex" needn't actually be terribly complex). It's as well to know at least something about mod_rewrite--if nothing else, to know when you might be able to best use it--and once you do, it tends to become habit to use it in all redirects.
     
    Owlcroft, Jul 18, 2004 IP
  8. batkerson

    batkerson Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I am unclear on something concerning this topic. I have some pages I used to promote my site (when I first started since I did not know what I was doing) and have since removed the pages and would like to implement a 301 redirect (I think). Currently, I am just using a ErrorDocument 404 http://www.quadaenterprises.com/locallinks.htm type of redirect. Well, that of course shows up as a temporary redirect which as I understand it tells the spiders that the pages that are no longer on my site are only gone temporarily. I would like to direct any requests for these "gone" pages to my home page. There are too many pages to list individually and they were all in their own folder so I was hoping I could just do this redirect 301 /doors http://www.quadaenterprises.com. I have tried this and it does not seem to work. Is there an easy way to make this work? Should the .htaccess be in the doors folder with that in it (it is currently in the root of the domain)?
    Thank you, Brent
     
    batkerson, Jul 21, 2004 IP
  9. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'm not sure I have the details correct, so if I have them wrong do please post the correct info. But, as I understand it, you want to redirect any call for a page that was in http://www.quadaenterprises.com/doors/ to the index page of that same site.

    You want the .htaccess file to be in your root. There are occasions for putting ancillary .htaccess files in subdirectories, but those are usually rather specialized needs.

    You should be able to get by with something like this:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^doors/(.*) http://www.quadaenterprises.com/ [R=301,L]​
    </IfModule>​

    If you try using the simpler mod_alias directive--

    Redirect 301 /doors http://www.quadaenterprises.com/

    --what I believe would happen is that a call for http://www.quadaenterprises.com/doors/foo.html would be redirected to a call for http://www.quadaenterprises.com/foo.html, which I gather is not what you are looking for.
     
    Owlcroft, Jul 21, 2004 IP
  10. batkerson

    batkerson Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Trying to clarify a little bit. I have several pages listed, at Google for instance (http://www.google.com/search?num=100&hl=en&lr=&ie=UTF-8&q=site%3Awww.quadaenterprises.com), that are www.quadaenterprises.com/doors/foo.htm. I would like to have any call for any file in the doors (also an extras) folder to go to www.quadaenterprises.com/used-for-sale.htm. My goal is to indicate to the spiders that these pages no longer exist and hopefully the search engines will unindex the pages. Meanwhile, I do not want to lose the traffic these "pages" are creating.
    So, I would use something like this in my .htaccess file (which is in the root directory of the site):

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^doors/(.*) ^extras/(.*) http://www.quadaenterprises.com/used-for-sale.htm [R=301,L]
    </IfModule>
     
    batkerson, Jul 22, 2004 IP
  11. batkerson

    batkerson Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you for your help. I have got things working the way I think I want them to. I will see what the search engines do with it over the next week or so.
     
    batkerson, Jul 23, 2004 IP
  12. ColinR

    ColinR Guest

    Messages:
    454
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #12
    A rookie here on 301 redirect ".htaccess" however wondering how hard it is to have a http://www.forums.domain.com to be http://forums.domain.com.

    Can anyone provide 301 re-direct .htaccess code or sample where I just change directory and domain names in a file with a bit of instructions? Thanks a bunch.
     
    ColinR, Jul 28, 2004 IP
  13. Synchronium

    Synchronium Active Member

    Messages:
    463
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #13
    I just moved the contents of one directory (first letter uppercase) to another (all lower case) including subfolders etc like so:

    RewriteRule ^News/?(.*) http://www.coffeesh0p.com/news/$1 [R=301,L]
    RewriteRule ^Shop/?(.*) http://www.coffeesh0p.com/shop/$1 [R=301,L]
    RewriteRule ^IRC/?(.*) http://www.coffeesh0p.com/irc/$1 [R=301,L]
    RewriteRule ^Info/?(.*) http://www.coffeesh0p.com/info/$1

    And so on.

    The $1 on the end of the URL appends anything extra after /dirname/ onto the new dir. EG /Shop/6/SA001/ would redirect to /shop/6/SA001/ on the new domain.

    Hope that helps.
     
    Synchronium, Aug 3, 2007 IP
    movingconcierge likes this.
  14. oacgo

    oacgo Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    That was SO helpful. Thank you. I had a temporary blog installation that got indexed by Google and I didn't want to lose those indexed pages, so I used this to refer them from /blog3 to /blog2 on the server. Big help. Thanks!
     
    oacgo, Dec 22, 2010 IP
  15. Merlin-Sweden

    Merlin-Sweden Well-Known Member

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    118
    #15
    Thanks for sharing your valuable info Synchronium.

    Do you think its possible to tweak your code to move an entire site to another domain or subdomain ( keeping the same structure of cource) ?

    maybe something like this ?

    RewriteRule ^/?(.*) http://www.newsite.se/$1 [R=301,L]

    /Vlad
     
    Merlin-Sweden, Jan 12, 2013 IP