I am beginner in PHP and Apache. I am trying to use URL Rewriting for my website, which can have following URIs.
1. http://localhost:8080/home
2. http://localhost:8080/dell
For the first URI, there is home.php file available and I edited .htaccess file with following rewrite rule.
RewriteRule ^([^\.]+)$ $1.php [NC]
Second URI is dynamic – means there is no dell.php available for it that could handle the request. So I wrote following RewriteCond.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ list.php?page=$1
So as a whole, my .htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^\.]+)$ $1.php [NC] [N]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ list.php?page=$1
So when I request localhost:8080/home, home.php is duly responded but when I request localhost:8080/dell, an error is responded
“The requested URL /dell.php was not found on this server.”
I think only first RewriteRule takes precedence and second one is not even read by Apache server. Changing order does not work either. My question here is what RewriteRule(s) should I use that could work in both case – PHP file exists & does not exist?
Thank you for your opinion and guidance.
Ritesh
You need to do a pre-emptive check against the
%{REQUEST_FILENAME}with a .php after it to see if that exists before you try to rewrite it, otherwise URI’s like dell will get blindly rewritten: