Htaccess 重写 URL 并传递 $_GET 参数
Htaccess rewrite URL with passing $_GET parameters
我一直在尝试通过在目录中显示 URL 来获得特定结果。我引用了 this and this post。我正在 PHP 从事一个项目。目前,我的 URL 参数如下:
https://subdomain.example.com/Dir/childDir/?id=12345
或 https://subdomain.example.com/Dir/childDir/index.php?id=12345
我要找的结果是
https://subdomain.example.com/Dir/childDir/12345
我可以在 PHP 中处理错误(即强制参数,否则重定向),但 Htaccess 总是让我头疼。目前,这是我的目录 Htaccess。
RewriteEngine On
RewriteBase /
RewriteRule ^childDir\/([^/]+)(/([^/]+))?$ index.php?method=
=====================================
编辑:已解决。谢谢@anubhava
RewriteEngine On
# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\d+)/?$ index.php?id= [L,QSA]
结果:
<? echo $_GET['id'] ?>
您可以在 Dir/childDir/.htaccess
:
中使用此规则
RewriteEngine On
# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\w+)/?$ index.php?id= [L,QSA]
我一直在尝试通过在目录中显示 URL 来获得特定结果。我引用了 this and this post。我正在 PHP 从事一个项目。目前,我的 URL 参数如下:
https://subdomain.example.com/Dir/childDir/?id=12345
或 https://subdomain.example.com/Dir/childDir/index.php?id=12345
我要找的结果是
https://subdomain.example.com/Dir/childDir/12345
我可以在 PHP 中处理错误(即强制参数,否则重定向),但 Htaccess 总是让我头疼。目前,这是我的目录 Htaccess。
RewriteEngine On
RewriteBase /
RewriteRule ^childDir\/([^/]+)(/([^/]+))?$ index.php?method=
=====================================
编辑:已解决。谢谢@anubhava
RewriteEngine On
# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\d+)/?$ index.php?id= [L,QSA]
结果:
<? echo $_GET['id'] ?>
您可以在 Dir/childDir/.htaccess
:
RewriteEngine On
# rewrite /Dir/childDir/12345 to /Dir/childDir/index.php?id=12345
RewriteRule ^(\w+)/?$ index.php?id= [L,QSA]