Htaccess and mod_rewrite make apache fail with missing file

For a simple php site, I want to have friendly URLs. Therefore, I would like my URLs to look like this:

http://mysite.com/page/123

Which indicates:

http://mysite.com/page.php?id=123

And it works! But if I point to a file that is not on the server, apache spikes, and I have to get Apache to exit (developing locally with MAMP, right now).

Here is my .htaccess file. Any ideas?

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([^/=]+)/?([^/]*)/?$ $1.php?id=$2 [N,QSA]

ErrorDocument 404 /404.php
+3
source share
1 answer

Just change the order of your two rules and use the L flag instead of N:

RewriteCond $1 !.*\.php$
RewriteRule ^([^/=]+)/?([^/]*)/?$ $1.php?id=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
+1
source

Source: https://habr.com/ru/post/1788646/


All Articles