SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htacces Question

    I'm using WAMP to serve a pet project on my localhost called "mvc." I'm trying to redirect requests made to mvc's root to a sub folder called /public. I have a .htaccess file in my root with the below code. It's not getting the job done.

    Code:
    RewriteEngine on
    #!-d .htaccess ignores all requests made directly to directories sub folders
    RewriteCond %{REQUEST_FILENAME} !-d
    #!-f .htaccess ignores all requests made to specific files within directory
    RewriteCond %{REQUEST_FILENAME} !-f
    #favicon.ico is a default file some browsers request. don't let browser trigger 404 if we don't have this file
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    
    RewriteBase /mvc/public
    
    #send all alphanumeric requests to index.php as QSA, second match catches file extension
    RewriteRule ^([-_A-z0-9\/]+)\.?([A-z]+)?$ /mvc/public/index.php?url=$1&extension=$2 [QSA,L]
    I have mod_rewrite enabled and I'm not getting any errors.

  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)
    bb,

    Quote Originally Posted by brandonBuster View Post
    I'm using WAMP to serve a pet project on my localhost called "mvc." I'm trying to redirect requests made to mvc's root to a sub folder called /public. I have a .htaccess file in my root with the below code. It's not getting the job done.

    Code:
    RewriteEngine on
    #!-d .htaccess ignores all requests made directly to directories sub folders
    RewriteCond %{REQUEST_FILENAME} !-d
    #!-f .htaccess ignores all requests made to specific files within directory
    RewriteCond %{REQUEST_FILENAME} !-f
    #favicon.ico is a default file some browsers request. don't let browser trigger 404 if we don't have this file
    RewriteCond %{REQUEST_URI} !=/favicon\.ico
    
    # technique?
    RewriteBase /mvc/public #send all alphanumeric requests to index.php as QSA, second match catches file extension RewriteRule ^([-_A-z0-9\/]+)\.?([A-z]+)?$ /mvc/public/index.php?url=$1&extension=$2 [QSA,L]
    # Really, from A (character 41) to a (character 7a)? # Frankly, I've never seen anyone do it this way before # (but it doesn't mean that it's wrong). # If you're specifying upper and lower case letters, please replace A-z with A-Za-z in both instances. # If the dot character is truly optional, then you have no file extension so there is also a logic problem.
    I have mod_rewrite enabled and I'm not getting any errors.
    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 Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [A-z] is shorthand for [A-Za-z] which works just as well and makes your code more readable. About the extension being optional, this is correct. I'm bootstrapping everything through the directory's index.php file.

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,272
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brandonBuster View Post
    [A-z] is shorthand for [A-Za-z] which works just as well and makes your code more readable.
    It may not have been the cause of your problem, but I think this trick is actually bad practice, because it deceptively includes characters you may not have intended, such as [ \ ] ^ _ `

    On to your issue. I copy-pasted your htaccess, but it worked correctly for me.

    To make sure we're on the same page, here is where the files were set up.

    Code:
        DocumentRoot/
            mvc/
                .htaccess
                public/
                    index.php
    "First make it work. Then make it better."

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    It may not have been the cause of your problem, but I think this trick is actually bad practice, because it deceptively includes characters you may not have intended, such as [ \ ] ^ _ `
    I wasn't aware of this. Thank you for pointing that out.

    Your directory tree is correct. That is how I have my project set up. After stating you were able to run the script successfully, I had to ensure mod_rewrite was in fact enabled. It is. The rewrite_module is being loaded and the AllowOverride rules are set accordingly.

    If anything, thank you at least for pointing out the specifics with the regex flaw. That may be the most important thing I learn all day!


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
  •