Changing file extensions with ModRewrite ?

Discussion in 'Apache' started by Mudder, Jul 21, 2008.

  1. #1
    Hi there,

    I need a solution to a ModRewrite problem I have. In its simplest form it will be a file extension rewrite.

    Example:

    I need to change the extensions of a great many files from PDF to XML. These files life in subdirectories under the following tree:

    http://www.mydomain.com/data/documents/12x456f/doc1.pdf

    There are hundreds of unique directories under /data/documents/

    I want to avoid having a ModRewrite for every /data/documents/???????/ directory. I want the /???????/ part to be a variable that is passed on to the rewritten URL so that the file extension is re-written to be:

    http://www.mydomain.com/data/documents/12x456f/doc1.xml

    And so forth for every instance of:

    http://www.mydomain.com/data/documents/???????/doc1.xml

    Any help in accomplishing this will be much appreciated.

    Kind regards

    Mudder
     
    Mudder, Jul 21, 2008 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    Maybe something like this:

    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^data/documents/([^/]+)/doc1.pdf$ /data/documents/$1/doc1.xml [L]
    
    Code (markup):
    or in the case your filename is also an variable then you should try something like this:

    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^data/documents/([^/]+)/([^/]+).pdf$ /data/documents/$1/$2.xml [L]
    
    Code (markup):
     
    pr0t0n, Jul 21, 2008 IP
  3. Mudder

    Mudder Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you. Your example works perfectly. I appreciate the assistance.
     
    Mudder, Jul 24, 2008 IP