1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MOD_REWRITE real expert challange

Discussion in 'Web Design' started by blackseo1, Jun 16, 2010.

  1. blackseo1

    blackseo1 Newbie

    Joined:
    Jan 15, 2010
    Messages:
    16
    Likes Received:
    2
    Hi, i am going crazy :eek: trying to solve this problem. I have following in my simplified .htaccess:

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !test.php
    RewriteRule ^(.+)$ /test.php?param=$1
    
    As you might guess the goal is to process any hxxp://example.com/*keyword* url with test.php script taking *keyword* as a parameter.

    This works perfectly UNLESS there is a directory called *keyword*. Then the RewriteRule is being ignored and execution goes directly into *keyword* directory.

    Any ideas how to fix this rewrite? (without removing/renaming *keyword* directory) :confused:
     
    Last edited: Jun 16, 2010
  2. ourxp

    ourxp Newbie

    Joined:
    Jun 18, 2009
    Messages:
    28
    Likes Received:
    1
    how about adding this statement:

    RewriteCond %{REQUEST_FILENAME} !-d
     
  3. CyrusVirus

    CyrusVirus BANNED BANNED Premium Member

    Joined:
    Aug 20, 2009
    Messages:
    1,110
    Likes Received:
    686
    how about locking the *keyword* directory to a chmod o 444
     
  4. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Try:

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ /test.php?param=$1 [R,L]
    
     
  5. blackseo1

    blackseo1 Newbie

    Joined:
    Jan 15, 2010
    Messages:
    16
    Likes Received:
    2
    this would prevent rule from being executed for *keyword* if *keyword* file or directory exists, while what I want is rule to be always execute even if *keyword* directory exists
     
  6. blackseo1

    blackseo1 Newbie

    Joined:
    Jan 15, 2010
    Messages:
    16
    Likes Received:
    2
    this almost equivalent to deleting the directory since it will prevent access to it from both apache and php. I would like that directory to be still aceessible. For example:

    /images/button1.png -> /images/button1.png (no rewrite)
    /images/button2.png -> /images/button2.png (no rewrite)
    /images -> /test.php?keyword=images
     
  7. fatboy

    fatboy Elite Member

    Joined:
    Aug 13, 2008
    Messages:
    1,618
    Likes Received:
    3,227
    Occupation:
    Retired
    Location:
    Old Peoples Home
    Sorry - read your original post at speed so got what you wanted twisted :)
     
  8. MuonTrail

    MuonTrail Registered Member

    Joined:
    Mar 5, 2009
    Messages:
    96
    Likes Received:
    93
    Try this:

    Code:
    RewriteEngine On
    
    # Lets your original rule work when there's an existing directory
    DirectorySlash Off
    
    # Keep files accessible.
    RewriteRule (.*)keyword/(.+) - [L]
    
    RewriteCond %{REQUEST_FILENAME} !test.php
    RewriteRule ^(.*)$ test.php?param=$1 [L]
    You may or may not want to turn the DirectorySlash off for your whole site, as it could be a security problem. (Read it here if curious.) The alternative is to move the DirectorySlash directive to a htaccess file in that one directory.

    And the next RewriteRule will keep all the files in that directory still accessible.

    Hope that helps.