Htaccess rewrite URL with passing $_GET parameters

Viewed 292

I have been trying to attain a particular result with the display of URLs within a directory. I have referenced this and this post. I'm working on a project in PHP. Currently, my URL with parameters is the following:

https://subdomain.example.com/Dir/childDir/?id=12345 OR https://subdomain.example.com/Dir/childDir/index.php?id=12345

The result I am looking for is https://subdomain.example.com/Dir/childDir/12345

I can take care of error handling in PHP (i.e. mandatory parameter, else redirect), but Htaccess has always gone over my head. Currently, this is my Htaccess for the dir.

RewriteEngine On
RewriteBase /
RewriteRule ^childDir\/([^/]+)(/([^/]+))?$ index.php?method=$1

=====================================

Edit: Solved. Thank you @anubhava

RewriteEngine On

# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\d+)/?$ index.php?id=$1 [L,QSA]

Result: Working example

<? echo $_GET['id'] ?>

1 Answers

You may use this rule in your Dir/childDir/.htaccess:

RewriteEngine On

# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\w+)/?$ index.php?id=$1 [L,QSA]
Related