Craig Craig - 1 year ago 148
Apache Configuration Question

Using RewriteCond with %{REQUEST_URI} for mod_rewrite

I would like the URL:

To point to:

My .htaccess file looks like this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^.+
#RewriteCond post/%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ post/$1.php [L]

This works however when I uncomment the line above it does not work.

I have tried:

RewriteCond /post/%{REQUEST_URI}.php -f
RewriteCond post/%{REQUEST_URI}.php -f
RewriteCond post%{REQUEST_URI}.php -f

None of these work. What am I doing wrong?

Also is there an easy way to debug these rules? Like console logging variables or something?

Answer Source

-f check requires full filesystem path. You can do this using %{DOCUMENT_ROOT} variable:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/post/$1.php -f
RewriteRule ^(.+)$ post/$1.php [L]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download