mod rewrite htaccess issue

Viewed 35

The bottom 4 rewrite rules work fine, but as soon as I add the first rule to make it so I can hide the .php extension from standard pages (eg about.php), then the last rule stops working?

With the last rule I wanted the URL to be domain.com/123 but the page is actually /offer.php?id=123

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^preview/([^/]*)$ /create-preview.php?id=$1 [L]
RewriteRule ^update/([^/]*)$ /update.php?id=$1 [L]
RewriteRule ^delete/([^/]*)$ /delete.php?id=$1 [L]
RewriteRule ^([^/]*)$ /offer.php?id=$1 [L]
1 Answers

Have it like this:

Options -MultiViews
RewriteEngine On

RewriteRule ^preview/([^/]+)/?$ create-preview.php?id=$1 [L,QSA,NC]

RewriteRule ^(update|delete)/([^/]+)/?$ $1.php?id=$2 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ offer.php?id=$1 [L,QSA]

Changes are:

  1. Disable MultiViews (content negotiation) as there are 2 cases where php file name is same as starting path name i.e. /update and /delete.
  2. Combining above 2 rules into one using regex
  3. Placing php extension rule after these rules and before last front controller rule
  4. Last front controller rule would need 2 RewriteCond to skip physical files and directories.
Related