Results 1 to 3 of 3
  1. #1

    mod_rewrite RewriteRule

    I'm going crazy trying to figure this out. After reading a tonne of posts and going through trial-and-error I've finally signed up so I can ask.

    There's a folder on our site with a bunch of PDF's available for download. Each year, the names are updated to reflect the new data. However there are external links pointing to the old files which I'd like to automatically redirect to the new ones. Hopefully all in one rewrite line.

    old file names:

    new filenames:

    Can somebody please tell me what the RewriteRule would be to achieve the result? ie, when somebody links in to XXXXXX1_StaticText_2009.pdf they automatically get XXXXXX1_StaticText_2011.pdf instead? An explanation of the workings of it would be very (VERY) much appreciated as well.

    Thanks for your time.


  2. #2
    Would it not be quicker and much simpler to just have the downloadable file the same?

    When a new one comes out archive (rename) the file and copy the new one in its place.

  3. #3
    Join Date
    Feb 2005
    You're looking for substitution of parts of the original request into the rewritten request, something like this (untested):

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !.*\.2011.pdf
    RewriteRule ^(.*)_StaticText_(.*)\.pdf $1_StaticText_2011.pdf [R=301,L]
    Line 1 turns on rewrites (if not already on)
    Line 2 is a condition that the request is not for a current file (since otherwise that would be rewritten too and you'd be in a rewrite loop).
    Line 3 looks for _StaticText_ and matches the strings each side of it. $1 and $2 will contain the substrings matched. Using a 301 redirect should help search engines to update their results and the final "L" indicates that after a match there will be no further processing of any rules that may come after this one.

    Lots more information and examples here.

    You'll probably also want to put in another condition to cover the case where the original request should be allowed because no 2011 document is available. Easiest way to do that is just to remove the old file when a new one is uploaded and redirect only if the old file is missing:
    RewriteCond %{REQUEST_FILENAME} !-f
    Last edited by foobic; 04-08-2011 at 06:42 PM.

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

Similar Threads

  1. Mod_Rewrite: RewriteRule to parent directory?
    By shockuk in forum Programming Discussion
    Replies: 3
    Last Post: 04-11-2007, 06:35 AM
  2. RewriteRule
    By Bambuk in forum Web Hosting
    Replies: 11
    Last Post: 07-21-2005, 05:31 AM
  3. RewriteRule Help
    By chrisS in forum Hosting Security and Technology
    Replies: 0
    Last Post: 11-24-2004, 12:13 AM
  4. RewriteRule mod_rewrite
    By Mb81 in forum Hosting Security and Technology
    Replies: 0
    Last Post: 10-03-2004, 02:05 PM
  5. Any one can help me in RewriteRule ?
    By justbenice in forum Hosting Security and Technology
    Replies: 2
    Last Post: 08-01-2004, 07:41 PM

Tags for this Thread

Posting Permissions

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