DivFellaPoor
DivFellaPoor

Reputation: 43

.htaccess Rewrite - Masking Dynamic .php extensions

I understand that similar questions have been asked before in regard to .htaccess, but after hours of reading through the Questions, Answers and Comments, as well as countless hours reading .htaccess documentation and even trying .htaccess generators... and messing my site access up via trying examples... I come to seek those wiser than me for guidance.

I am using the following .htaccess file to remove .php extensions from URLS shown to the user in the browser

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

This works perfectly
www.example.com/users/player.php?id=1 is being correctly shown as www.example.com/users/player?id=1

I am now struggling to write/find/understand examples that will allow me to rewrite the full extension

I want to show
www.example.com/users/player.php?id=1
as
www.example.com/users/1
totally removing the player.php?id= part of the dynamic URL

Thank you in advance for any help/guidance.

Upvotes: 0

Views: 74

Answers (1)

DivFellaPoor
DivFellaPoor

Reputation: 43

For anyone else who wants to do what I wanted to do, I have found a solution.

Question: Htaccess rewrite with id

Answer: from Anubhava

This will allow you to externally mask "edit.php?id=1" to "edit/id/1" as well as use either URL interchangeably.

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]

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

Best of luck!

Upvotes: 1

Related Questions