Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用.htaccess创建具有多种字符的奇特URL_Php_.htaccess - Fatal编程技术网

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

  • 如果我将
    -
    添加到方括号中,并将其作为
    a
    等于404的字符会出现错误
  • 如果我将
    添加到方括号中,我将得到
    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]