SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing underscores with slashes in htaccess

    I'm curently working on a very old site. The site contains a lot of "static" php pages.

    A group of static pages is named like this:

    program_programnameX.php
    program_programnameY.php
    program_programnameX_subpage.php
    program_programnameZ.php
    They are all located in the root map of the website (I know, dirty, but like I said: old site!)

    Now I want to make the url's a bit more readable like: http://www.mysite.com/program/programnameX/ http://www.mysite.com/program/programnameY/

    Both the old and new links should still be able to work and I only want to affect the pages starting with "program_".

    This is the htaccess code I came up so far:

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(program_)([a-z\-]*)$ $1$2.php [QSA,L]
    But I can't seem to get it working.
    Any ideas?

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

    ARGH! Reverse your thinking as YOU create the links in the format you want visitors to see then YOU create the mod_rewrite code to turn that into something Apache can serve.

    What your code SHOULD be attempting is replacing /'s with _'s, checking for -f then redirecting to the serveable format (without the 301 which would cause the redirection to be seen). However, USE the R=301 flag to test so you can see that the mod_rewrite code is working.

    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

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for getting me on track. I now have 2 parts of code that work seperatly ok, but when I combine them I get an error saying there are too many redirects.

    Code:
    RewriteEngine on
    
    #if the user brows to the site with an old url (program_foo.php) redirect him to a clean url (http://localhost/program/foo)
    RewriteRule ^program_([a-z\-]*).php$  http://localhost/program/$1  [R=301,L]
    
    #if the user uses a clean url, tell the server what the actual filename is
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^program/([a-z\-]*)$ http://localhost/program_$1.php
    Not sure what's causing this because I'm a complete htaccess noob.

    Your point of naming the files the correct way is valid, although this site I'm working on is very old. And I'm just searching for a quick patch untill the new website launches this summer.

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

    Quote Originally Posted by Shaddix View Post
    Thanks for getting me on track. I now have 2 parts of code that work seperatly ok, but when I combine them I get an error saying there are too many redirects.

    Of course it is - you've gone loopy!

    Okay, that's what you've directed Apache to do so I stand corrected!

    Troubleshooting: Follow the URI through the logic of the mod_rewrite code.

    Code:
    RewriteEngine on
    
    #if the user brows to the site with an old url (program_foo.php) redirect him to a clean url (http://localhost/program/foo)
    RewriteRule ^program_([a-z\-]*).php$  http://localhost/program/$1  [R=301,L]
    # sends program_programnameX.php to program/programnameX
    # WHY are you using the absolute (external) redirection?
    # and restarts the second pass which gets to this point unaltered
    
    #if the user uses a clean url, tell the server what the actual filename is
    RewriteCond %{REQUEST_FILENAME} !-d
    # presumably, programnameX isn'd a directory
    RewriteCond %{REQUEST_FILENAME} !-f
    # no file extension so no match on file
    RewriteRule ^program/([a-z\-]*)$ http://localhost/program_$1.php
    # escaping the - isn't necessary but it should be the first character in the range definition (but last seems to be accepted, too
    # program/programnameX is matched and redirected back to program_programnameX.php
    # and a third pass is initiated which follows the same path to redirect until Apache gets exasperated and quits redirecting

    Not sure what's causing this because I'm a complete htaccess noob.

    That's no excuse as it's simply logic. Did you follow your own logic above? Do you see the loop?

    Your point of naming the files the correct way is valid, although this site I'm working on is very old. And I'm just searching for a quick patch untill the new website launches this summer.

    If you want to retain PR, you'll need to build the renaming pattern into your new website or you'll start fresh with SE's and waste all the goodwill your site's built over the years. Wouldn't that be a silly thing to do?

    Have a read through the tutorial linked in my signature for more information on mod_rewrite from enabling it in your test server to coding to providing sample code for routine tasks.
    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 Member
    Join Date
    Feb 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for pointing that out!

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

    That question was asked of me 2-3 years ago and the answer (actually, the one I came up with and a correct one) is discussed with the code (for both) in the tutorial linked in my signature. In other words, what you've attempted CAN be done but it's slightly abusive of the server and MUST be done correctly (to avoid the looping). Let me know if you have any questions on either solution.

    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
  •