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/.
-
Does this answer your question? .htaccess rewrite php to php fileLuke Briggs– Luke Briggs2022-04-29 02:37:11 +00:00Commented Apr 29, 2022 at 2:37
Add a comment
|
1 Answer
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.
- Using the variable
1 Comment
DevSixl
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.