Php 使用.htaccess创建具有多种字符的奇特URL
我想使一个网址看起来赏心悦目Php 使用.htaccess创建具有多种字符的奇特URL,php,.htaccess,Php,.htaccess,我想使一个网址看起来赏心悦目 from /index.php?a=grapes to /grapes 虽然,我有一些问题。我希望a有更多种类的字符,比如a-z a-z 0-9/-。[] from /index.php?a=Grapes.Are.Green/Red[W4t3r-M3l0n_B1G_Gr4p3] to /Grapes.Are.Green/Red[W4t3r-M3l0n_B1G_Gr4p3] 在index.php文件中,我有 <?php
from
/index.php?a=grapes
to
/grapes
虽然,我有一些问题。我希望a
有更多种类的字符,比如a-z a-z 0-9/-。[]
from
/index.php?a=Grapes.Are.Green/Red[W4t3r-M3l0n_B1G_Gr4p3]
to
/Grapes.Are.Green/Red[W4t3r-M3l0n_B1G_Gr4p3]
在index.php文件中,我有
<?php
$a = $_GET["a"];
echo $a;
?>
仅接受a-z a-z 0-9/\uz
- 如果我将
添加到方括号中,并将其作为-
等于404的字符会出现错误a
- 如果我将
添加到方括号中,我将得到
输出index.php
- 如果我添加
或[
我会得到404错误]
如果有人有解决办法,我很想看看。另外,如果有人有时间,请您解释一下规则的每一部分,说明该部分的作用。谢谢 问题是你的某些角色是“特殊的”: 特殊字符:
(full stop) - match any character
* (asterix) - match zero or more of the previous symbol
+ (plus) - match one or more of the previous symbol
? (question) - match zero or one of the previous symbol
\? (backslash-something) - match special characters
^ (caret) - match the start of a string
$ (dollar) - match the end of a string
[set] - match any one of the symbols inside the square braces.
(pattern) - grouping, remember what the pattern matched as a special variable
因此,如果您想在url中使用它们,您必须替换它们
比如说
.s?html?匹配“.htm”、“.shtm”、“.html”或“.shtml”
最后的
[QSA]
是它成功的原因:)感谢jedwards建议使用接受所有字符的^(.*)$
。您是否尝试过接受所有字符的^(.*)$
(未测试,可能有点不合适)之类的方法?有什么东西会让你不想接受所有字符吗?@jedwards-如果我使用^(.*)$
我会得到index.php
输出。我刚刚读了一点关于它的内容,我使用了.htacces刚才谢谢你的回答!我现在正在使用RewriteRule^([a-zA-Z0-9/\-\[\]]+)?$index.php?a=$1
,它允许额外的-[]
。如何将“.”添加到集合中?我尝试过反斜杠,但还是得到了index.php
输出。
(full stop) - match any character
* (asterix) - match zero or more of the previous symbol
+ (plus) - match one or more of the previous symbol
? (question) - match zero or one of the previous symbol
\? (backslash-something) - match special characters
^ (caret) - match the start of a string
$ (dollar) - match the end of a string
[set] - match any one of the symbols inside the square braces.
(pattern) - grouping, remember what the pattern matched as a special variable
RewriteEngine On
RewriteRule ^(.*)$ index.php?a=$1 [QSA]