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/.

1

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.