使用 .htaccess 重写 URL 不传递参数 [英] URL rewriting with .htaccess not passing parameter
问题描述
我正在使用 php.我想将每个请求重定向到 index.php 文件并在那里访问参数,例如, mysite.com/a 应该重定向到 mysite.com/index.php?id=a 以便我可以使用访问我的代码中的参数$_GET["id"], 为此,我使用以下代码创建了一个 .htaccess 文件.
I am using php. I want to redirect every request to the index.php file and access parameters there, for example, mysite.com/a should redirect to mysite.com/index.php?id=a so that I can access the param in my code using $_GET["id"], To do this, I have created a .htaccess file with following code.
Options +FollowSymlinks
RewriteEngine on
RewriteRule [a-z] index.php?id=$1 [NC]
注意:这里我考虑每个请求都用小写字母.
note: here I am considering every request with a lowercase letter.
我正在尝试像这样访问我的 index.php 文件中的 id:
I am trying to access id in my index.php file like this:
<?php
echo "sample redirection: <br/>";
echo $_GET["id"];
?>
这是重定向但不传递参数.所以我的输出是这样的:
This is redirecting but not passing the parameter. so my output is like this:
sample redirection:
它不打印参数.帮助.
推荐答案
[a-z]
后面需要一个美元符号.
You need a dollar sign after the [a-z]
.
RewriteRule ([a-z])$ index.php?id=$1 [NC]
注意 [a-z]
只会匹配一个字母.要匹配任何字母字符串,请使用:
Note that [a-z]
will only match ONE letter. To match any string of letters, use:
RewriteRule ([a-z]*)$ index.php?id=$1 [NC]
这篇关于使用 .htaccess 重写 URL 不传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!