隐藏GET请求中的网址参数

时间:2018-11-25 11:24:32

标签: .htaccess

我想使用$_GET.htaccess请求中隐藏一些网址参数。我尝试了不同的方法,当然也可以通过以下链接进行操作:How to hide params passed in url using .htaccess

但是对我来说失败了:

Error image - hidden id

我的.htaccess代码:

RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
RewriteRule ^([0-9a-zA-Z]+)/?$ /$1.php

但这仅在添加id时有效:

With ID works

我想隐藏id并像这样显示它:

localhost/get_test.net/test/4

更新: 因此,它适用于以下代码:

RewriteBase /
RewriteEngine On
RewriteRule ([^/\.]+)/?$ test.php?id=$1 [L]

现在,我需要将test.php更改为任何链接,并通过index.php页面解决该问题,因为找不到该页面:

Index page not found

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

如果您的文件结构是这样的(来自屏幕截图):

/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]

在正则表达式字符类中使用时,无需转义文字点。

更新:上面的方法还处理了URL末尾的/<id>被省略的情况。例如。 /index被重写为/index.php?id=(不是空的URL参数)。另请注意,如果提供了斜杠,则ID也必须包含1个或多个数字。