简体   繁体   English

Htaccess 通过 $_GET 参数重写 URL

[英]Htaccess rewrite URL with passing $_GET parameters

I have been trying to attain a particular result with the display of URLs within a directory.我一直在尝试通过在目录中显示 URL 来获得特定的结果。 I have referenced this and this post.我已经引用了这个这个帖子。 I'm working on a project in PHP.我正在 PHP 中开展一个项目。 Currently, my URL with parameters is the following:目前,我的 URL 参数如下:

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

The result I am looking for is https://subdomain.example.com/Dir/childDir/12345我正在寻找的结果是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.我可以处理 PHP 中的错误处理(即强制参数,否则重定向),但 Htaccess 总是超出我的想象。 Currently, this is my Htaccess for the dir.目前,这是我的目录的 Htaccess。

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

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

Edit: Solved.编辑:已解决。 Thank you @anubhava谢谢@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 :您可以在Dir/childDir/.htaccess中使用此规则:

RewriteEngine On

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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