Php 重写URL有什么问题?
我想要(例如) 输出Php 重写URL有什么问题?,php,apache,mod-rewrite,Php,Apache,Mod Rewrite,我想要(例如) 输出 discussPage.php?id=1&title=where-are-you 我在.htaccess文件中有以下命令,该文件与discussPage.php位于同一目录中: # Enable Rewriting RewriteEngine on # Rewrite user URLs # Input: discuss/ID/title/TITLE # Output: discussPage.php?tid=NAME&title=TITLE
discussPage.php?id=1&title=where-are-you
我在.htaccess
文件中有以下命令,该文件与discussPage.php
位于同一目录中:
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: discuss/ID/title/TITLE
# Output: discussPage.php?tid=NAME&title=TITLE
RewriteRule ^discuss/(\w+)/(\w+)/(\w+)/?$ discussPage.php?id=$1&title=$3
现在,当我试图访问像discussion/1/title/where are you
这样的页面时,我遇到了一个404
错误,即使discussPage.php?id=1&title=where you's work很好。
另外,启用了mod_rewrite(我已将其用于其他目的)。要进行调试,您可以始终
echo$\u服务器['QUERY_STRING']代码>
但我相信正确的代码应该是这样的
RewriteEngine on
RewriteBase /
RewriteRule ^discuss/([0-9]+)/title/([a-z0-9\-]+)?$ discussPage.php?id=$1&title=$2 [NC,QSA]
不过,您可能需要修复您的基础值
另外-如上一个条件所示,在您在哪里
零件中只能有字母数字字符和连字符。(但有消息告诉我您不需要其他字符!)您的问题是\w与最后一位中的“-”不匹配
您可以简单地使用更广泛的字符类(如[\w-]+
)在其中允许破折号。我会小心使用像[^/]
这样的解决方案,因为它们允许除斜杠之外的任何东西,斜杠是非常允许的。白名单总是更安全,避免意外
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: discuss/ID/title/TITLE
# Output: discussPage.php?tid=NAME&title=TITLE
RewriteRule ^discuss/(\w+)/(\w+)/([\w-]+)/?$ discussPage.php?id=$1&title=$3
请注意,-
必须是字符类中的最后一个字符,否则您应该对其进行转义,因为如果它位于其他两个字符之间,它的行为就像一个范围,就像在[a-z]
中对我适用一样:
RewriteEngine On
RewriteBase /
RewriteRule ^discuss/([^/]+)/([^/]+)/([^/]+)/?$ discussPage.php?id=$1&title=$3 [R]
@特内夫:差不多了。正如其他人所说,您没有注意到\w不匹配-。不管怎样,algorithmicCoder今天情绪低落。
RewriteEngine On
RewriteBase /
RewriteRule ^discuss/([^/]+)/([^/]+)/([^/]+)/?$ discussPage.php?id=$1&title=$3 [R]