SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot theslip's Avatar
    Join Date
    Jun 2008
    Location
    here
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Set the Cache-Control and Expires Headers on a 301 Redirect? help please, please

    Hi All,

    I'm new to realising that browsers especially new ones are now caching 301 redirection and knowing how things go, changes of mind, moving files about, I'm not happy about using a 301 for redirection as I'm scared it will cache and I loose control of the url if I need to change it, now I always thought it the right way to 301 redirect pages and directories when renaming and such as it passes the juice to Search engines. I figure 302 is not the right way to go unless a vanity url for a changing landing page or something you know for sure will change. This being the case any ideas how to set cache control on the 301 to not cache? I normally do my 301's in htaccess like so (example of directory name changes/redirect):

    redirect 301 /pages/yellowpages http://www.mydomainname.com/pages/redpages
    redirect 301 /pages/pinkpages http://www.mydomainname.com/pages/whitepages

    Any ideas how I can add cache control to stop browsers caching my 301 so it always checks my htaccess for changes?

    Any help will be massivly appreciated.

    Thanks


  2. #2
    Hibernator YuriKolovsky's Avatar
    Join Date
    Nov 2007
    Location
    Malaga, Spain
    Posts
    1,072
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    and you are right to be afraid, some time ago browsers started to cache 301 redirects by default, and once it's cached you have no control or way to remove it,
    here is an article about it http://getluky.net/2010/12/14/301-re...nnot-be-undon/
    so you should specifically specify that the redirect should not be cached

    basically I think you need to send these headers to achieve what you want.

    Code:
    HTTP/1.1 301 Moved Permanently
    Cache-Control: no-store, no-cache, must-revalidate
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Location: {new location}
    in php you can do it like this
    Code:
    header('HTTP/1.1 301 Moved Permanently');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
    header("Location: {new location}");
    exit;
    or better like this
    Code:
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
    header("Location: {new location}", true, 301);
    echo '<a href="{new location}">Continue</a>';
    exit;
    where {new location} is an absolute path to the destination, with php relative URL will work too even though it's not correct according to the specification.

    in your case you add the following headers to this page http://www.mydomainname.com/pages/yellowpages
    Code:
    HTTP/1.1 301 Moved Permanently
    Cache-Control: no-store, no-cache, must-revalidate
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Location: http://www.mydomainname.com/pages/redpages
    which can be done in .htaccess with these rules
    Code:
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    redirect 301 /pages/yellowpages http://www.mydomainname.com/pages/redpages
    redirect 301 /pages/pinkpages http://www.mydomainname.com/pages/whitepages
    I'm really not sure if the above is correct as I have never done redirects using apache, but I bet it will work fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •