.htaccess并过滤$ _GET

Jon*_*nny 5 .htaccess mod-rewrite url-rewriting filter-var

您好我正在编写一个配置文件页面脚本,在此脚本中我检查传入的$ _GET变量的值并验证它是一个整数,然后我根据$ _SESSION值验证此值以确认它们只能访问自己的帐户.代码如下所示:

// validate $_GET field 
if(isset($_GET['identity']) && filter_var($_GET['identity'], FILTER_VALIDATE_INT, array('min_range' => 1))) {


if(isset($_SESSION['user_identity']) && ((int)$_SESSION['user_identity'] === (int)$_GET['identity'])) { // if session exists and is === $_GET['identity']
// Proceed with code
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,例如,如果我尝试传递'0','2-2','abc'或没有值作为$ _GET值,查询正确失败并将它们重定向到主页.

然后我尝试做的是改变我的.htaccess文件,将URL映射到'profile/1',只是为了整理它.

RewriteRule ^profile$ profile.php
RewriteRule ^profile/([0-9]+)$ profile.php?identity=$1 [NC,L]
Run Code Online (Sandbox Code Playgroud)

我现在发现的是,页面不再使用上面那些无效的$ _GET参数重定向.它只是试图找到'profile/abc.

有谁知道为什么?

Boo*_*eus 10

我用它,它对我有用:

RewriteEngine On
RewriteBase /
RewriteRule ^profile$ profile.php
RewriteRule ^profile/([a-z0-9\-]+)$ profile.php?identity=$1 [NC,L,QSA]
Run Code Online (Sandbox Code Playgroud)

现在,你是怎么得到的profile/abc?如果你试图在规则中传递字母它不会工作,因为你只指定数字([0-9]+).如果你想传递信件,你需要使用:

RewriteRule ^profile/([a-z0-9\-]+)/?$ profile.php?identity=$1 [NC,L,QSA]
Run Code Online (Sandbox Code Playgroud)