SitePoint Sponsor

User Tag List

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

    Mod rewrite - File to Directory Mapping

    I am using the following code in my htaccess file which is almost getting the effect I want (which is for directories to be mapped to my PHP script).

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule (.*)/$ index.php?r=$1

    This allows me to have URLs like example.com/dir/ to be mapped to example.com/index.php?r=dir

    The problem is that if I don't fully qualify the URL with a trailing slash i.e. example.com/dir I just get a page not found error.

    How can I change this behavior so that this works properly? Perhaps example.com/dir could redirect to example.com/dir/ in the same way that Apache would do that by default if dir was actually a directory?

    Thanks for your help

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

    Your regex is BAD.

    First, (.*) should (almost) never be used - it causes more problems than it's worth!

    Second, if you don't use / as a "marker," index.php will get matched, too (beginning to see the regex problem?).

    IMHO, a read of the tutorial Article linked in my signature will help you, too. In fact, your problem is so simple that the code will be there!

    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 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks dklynn. I have since updated my script to:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([a-zA-Z0-9-_]+)/$ index.php?r=$1 [L]

    This works a little bit better, and also stops real files and directories being included but having looked at your article I can't relate a solution to the problem I have. Having looked at markers, and reading the part about the performance overhead, that's something I want to avoid.

    If you could offer some more help, that would be great.

    By the way, the 'click to show/hide text' links in the article don't work in Safari (3.04 on Mac).

    Edit
    I don't know if this is the most elegant working solution but I have come up with:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([a-zA-Z0-9-_]+)(/?)$ index.php?r=$1

    Thanks

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

    Yes, I've heard that Safari doesn't like the show/hide. Thanks!

    If you're not using the trailing slash OR just want to make it optional, you DON'T need to make an atom out of it ( make "(/?)" just "/?" ).

    Using the / as a "marker" was okay but using the character range takes care of that for you (it doesn't have a dot character in it so it can't match index.php).

    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 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! You've helped a lot. I also found that on one server I tried this on Apache didn't like [a-zA-Z0-9-_]... I had to change it to [-_a-zA-Z0-9]. Weird!

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

    Hmmm, that seems like a repeat of the one I just answered:
    Quote Originally Posted by DK
    When you use a metacharacter within a range definition, it must be used correctly. Your use of the hyphen (dash) after the Z implies the start of a new range (from Z to _) which is not possible. I've repeatedly said that sources require the hyphen to be the first character but some allow it as the last character - it works - so it is not mistaken as part of a range definition.
    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
  •