我想使用来从$_GET
请求中隐藏一些url参数.htaccess
。我尝试了不同的方法,当然也可以从此链接中进行尝试:如何隐藏使用.htaccess在url中传递的参数
但这对我失败了:
我的.htaccess
代码:
RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
RewriteRule ^([0-9a-zA-Z]+)/?$ /$1.php
但这仅在id
添加时有效:
我想隐藏id
并显示为:
localhost/get_test.net/test/4
更新:因此,它适用于以下代码:
RewriteBase /
RewriteEngine On
RewriteRule ([^/\.]+)/?$ test.php?id=$1 [L]
现在,我需要更改test.php
为任何链接并解决index.php
页面问题,因为找不到该链接:
有任何想法吗?谢谢。
如果您的文件结构是这样的(来自屏幕截图):
/get_test.net
.htaccess
test.php
index.php
然后重写/get_test.net/<file>/<id>
为/get_test.net/<file>.php?id=<id>
,其中<id>
是数字,并且/<id>
是可选的,那么您需要执行以下操作:
# Prevent conflict with mod_negotiation
Options -MultiViews
RewriteEngine On
# Do NOT set RewriteBase (or set it to the subdirectory)
#RewriteBase /get_test.net
# Rewrite /<file>/<id> to <file>.php?id=<id>
RewriteRule ^([^/.]+)(?:/(\d+))?$ $1.php?id=$2 [L]
在正则表达式字符类中使用时,无需转义文字点。
更新:上面还处理/<id>
了URL省略尾部的情况。例如。/index
被重写/index.php?id=
(不是空的URL参数)。还请注意,如果提供了末尾斜杠,则ID也必须包含1个或多个数字。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句