SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Maryland, USA
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rewrite changed directory and file names

    Good Morning all,

    I need a little help with a rewrite statement. I have a directory of documents that I import from another network, recently when updating them I found the naming conventions had changed. This is wreaking havoc on links from other parts of the intranet and our internal search engine. There are a few hundred files so renaming would be a pain, and who knows what the document owner will do in the future. Here is what I have:

    The old naming was policy13/policy13_manual.html

    The new naming is policy_1-3/policy_1-3_manual.html

    This change applyes to every document in the directory.

    Any help would be greatly appreciated.
    R
    MH

  2. #2
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you can split 13 into 1 and 3 using mod_rewrite unfortunately. It could probably be done using four or 5 rules which would allow for up to 99999 documents but that's not a scalable solution. I'll have a think

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Maryland, USA
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dean,

    The numbers of the documents won't change. The way this is structured is like this

    Directory:

    policy1-3

    Files under that directory are named something like this:

    policy1-3_manual.html
    policy1-3_appendixa.html
    policy1-3_appendixb.html

    they used to be like this

    Directory:
    policy13

    Files under that directory were named something like this

    policy13_manual.html
    policy13_appendixa.html
    policy13_appendixb.html

    Hope that makes it more clear.
    R
    MH

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    mrhatch,

    Yes, you can do this with mod_rewrite as you merely apply regex to your URL.

    It appears that you merely want to split the digits with hyphens and set them off with underscores.
    Code:
    RewriteEngine On
    RewriteRule ^policy([0-9])([0-9])/policy([0-9])([0-9])([a-z]+)\.html policy_$1-$2/policy_$3-$4_$5.html [L]
    That regex is specifically designed to handle policy13/policy13_manual.html => policy_1-3/policy_1-3_manual.html where manual can also be any set of lowercase letters. The downside is that the digits MUST be single digits and policy is specified.

    Simple regex is all it takes!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Maryland, USA
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    Thank GOD for people who speak fluent regex. I have a hard enough time with English (read American) most days. A couple of tweaks and it works great on the development server, alas still not working on the production side. I am sure my wonderful Sysadmins have buggered up the server somehow. I did add the following though:



    Code:
    ^policy([0-9])([0-9])/policy([0-9])([0-9])([a-z]+)\.html /policy_$1-$2/policy_$3-$4_$5.html [R=301,L]
    That way it force the browser to give the "fixed" url for people and search engines to bookmark.

    Mike
    R
    MH

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mike,

    Are you sure your production server allows you to use .htaccess and the mod_rewrite module? I'd suggested an extremely simple test about a month ago:

    .htaccess:
    RewriteEngine On
    RewriteRule ^test.html$ test.php [L]

    Upload (making sure that you're using ASCII mode) and just have a test.php script to say that you were successful (<?php echo "<h1>Test Script - it works!</h1> ?>). Upload that as test.php (in ASCII mode) and try to load it by loading test.html.

    Yes, regex is a VERY useful tool! Enjoy!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Maryland, USA
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    I am embarrased to say it, but I was working with the wrong .htaccess server. They moved from one machine to another and I still had the old drive mapped. Doh! Put it in the right place and it works like a charm!

    Mike
    R
    MH

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mike,

    @#$% happens - don't worry about it! I do a lot of computer work VERY late at night and am occasionally (more often than I'll admit to!) make the same sort of errors. So long as we got everything sorted out for you, that's what counts!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •