MOD_REWRITE real expert challange

blackseo1

Newbie
Joined
Jan 15, 2010
Messages
16
Reaction score
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:
how about adding this statement:

RewriteCond %{REQUEST_FILENAME} !-d
 
Try:

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

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

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
 
how about locking the *keyword* directory to a chmod o 444

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
 
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

Sorry - read your original post at speed so got what you wanted twisted :)
 
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.
 
Back
Top