I have the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
RewriteRule ^([^\.]+)/([^\.]+)/$ $1.php?id=$2
I had problems with with the absolute URI, it solved. Now I want to use the last row for the following:
domain.com/query/ping/2/
to
domain.com/query/ping.php?id=2
I think the code is good, but I still get back error 404. Should I give some rewrite conditions? I searched for this code but haven’t found anything useful.
Based on where you’ve placed your htaccess file in your previous question, you probably want something like this:
The important issue is that you need to check for
/queryin the beginning of the URI. What you have,^([^\.]+)/([^\.]+)/$won’t match the/querypart along with the 2 pathnames after it. Your regex only matches 2 pathnames.The line
RewriteCond %{DOCUMENT_ROOT}/query/%1.php -fis similar to the condition you have above where it checks to see if the requested PHP file actually exists, otherwise it won’t blindly rewrite. This condition ensures if someone tries to go to:http://domain.com/query/blahblahblahblah/blahYour server won’t return a 404 error saying
/query/blahblahblahblah.phpdoesn’t exist.