SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question htaccess MOD_REWRITE URL underscores to slashes filenaming convention

    I'm working with a semi-old site that has used underscores throughout the base web directory. (how_to_do_this.php, who_is_author_name.php, etc.) Part of the redesign we are going to have categories (how-to, who-is) just as an example. To keep everything organized in the base directory we were going to change the naming convention for the URL to "folder-in-url / file-name-in-url.php" with filenames as "folder-in-url_file-name-in-url.php". Notice the underscore is the seperater for the URL so the stuff before the underscore is essentially a folder according to the URL.

    MORE EXAMPLES:
    ORIGINAL FILENAME -> RENAMED FILENAME -> URL
    folder_in_url_file_name_in_url.php -> folder-in-url_file-name-in-url.php -> /folder-in-url/file-name-in-url.php
    how_to_do_this.php -> how-to_do-this.php -> /how-to/do-this.php
    who_is_author_name.php -> who-is_author-name.php -> /who-is/author-name.php


    Is this possible using .htaccess / mod-rewrite? In other words, when someone goes to example.com/how-to/do-this.php then apache will look and present the content of the file "how-to_do-this.php" rather than looking in the how-to folder for do-this.php? Please note that when someone goes to the example.com/how-to/do-this.php it does NOT redirect them to example.com/how-to_do-this.php.

    Any help is greatly appreciated. At the end of the day, we can make the folders and put the files in their appropriate folders but then the base web directory will be chuck full of folders whereas for maintenance it would be easier just to administer within the base directory using appropriate filenames with an underscore separating for URL purposes.

    Best practices is preferred, so if this is a "hack" and not recommended then we can do folders and put the files within the appropriate "category" folders. Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the way to do this in .htaccess but still wondering if this is a good practice.

    Here is my htaccess:
    RewriteRule ^([a-z-]+)/([a-z-]+)(.*)$ $1_$2$3

  3. #3
    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)
    fv,

    What you use as a marker is (almost) irrelevant ... so long as it can't be confused with adjacent character sets. What you have looks okay (but will still have to be redirected to your handler file afterward).

    That said, I'm REALLY an advocate of eliminating the indiscriminate use of the EVERYTHING (or NOTHING) atom as it will generally create loopy code. In your case (from the example, (.*) is simply used to capture .php so why not use \.php? That's far more succinct and will eliminate any potential attack vectors, too. When you can use code to restrict requests to what your scripts can accept (as valid input), you're far ahead of the game.

    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
  •