3

I would like the URL:

http://domain.com/category/subcategory/title

To point to:

http://domain.com/posts/category/subcategory/title.php

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?

Craig
  • 2,093
  • 3
  • 28
  • 43

2 Answers2

6

-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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

I guess you mean the line

RewriteCond post/%{REQUEST_URI}.php -f

REQUEST_URI is

The path component of the requested URI, such as "/index.html".

post/%{REQUEST_URI}.php is then some relative path from where the Apache web server is running. This might be the root directory /, Apache's configuration directory /etc/apache2, its document root /var/www/html, or even /var/tmp, or anything else.

  • /post/%{REQUEST_URI}.php
  • /etc/apache2/post/%{REQUEST_URI}.php
  • /var/www/html/post/%{REQUEST_URI}.php
  • /var/tmp/post/%{REQUEST_URI}.php
  • /home/olaf/post/%{REQUEST_URI}.php

As you see, you can't really know what file Apache will look at. And Apache might find a file at this place, or it may not. To be sure, you must provide an absolute path as @anubhava already suggested.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198