0

I've been searching for how to do this, and I haven't been able to get anything to work yet. I want to remove the '.php' file extension from all files that have it while also adding a trailing slash and validating all the parameters following it. For example, I want my/directory/users/USER_ID/ to function the same as my/directory/users.php/USER_ID/.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • Does this answer your question? [.htaccess rewrite php to php file](https://stackoverflow.com/questions/22951819/htaccess-rewrite-php-to-php-file) – Luke Briggs Apr 29 '22 at 02:37

1 Answers1

0

As you reference Apache, here's one way of doing it in your .htaccess:

RewriteEngine On
RewriteRule ^my/directory/users/([0-9]+)/$ my/directory/users.php/$1/ [L]
  • Look for incoming URLs which match the regex ^my/directory/users/([0-9]+)/$.
    • ^ means 'start of the url'
    • $ means 'end of the url'
    • [0-9]+ means 'at least one digit here'
    • The brackets tell it to capture that digit series as a variable. It's the only variable we create, so it'll end up as $1.
  • If we match on that URL, we'll rewrite it to my/directory/users.php/$1/ and then stop considering any other rewrite rules, because this is a [L]ast one.
    • Using the variable $1, the previously captured digit series.
Luke Briggs
  • 3,745
  • 1
  • 14
  • 26
  • Thank you for this concise answer, I also want to remove the ```.php``` file extension from any URL on my page that has it and I was able to achieve that but when attempting to use a trailing slash and parameters with those rules, those pages are seen as directories. For example, ```my/directory/users``` would work but ```my/directory/users/1``` would not. – DevSixl Apr 29 '22 at 21:20