简体   繁体   中英

Htaccess rewrite URL with passing $_GET parameters

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 (ie 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: 工作示例

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM