从 url 获取变量,无法回显完整字符串

发布于 2024-12-09 14:57:45 字数 448 浏览 1 评论 0原文

我有一个动态网址,如下所示;

news/categories/s/23-brook-launch-new-zealand

“23”是文章 ID,“brook-launch-new-zealand”是文章标题。

当我这样做时:

echo $_GET['title'];

它只会回显:“brook”而不是完整的字符串。

我正在对我的网址使用 mod_rewrite,代码如下:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

如何回显完整字符串?

I have a dynamic url which looks like the following;

news/categories/s/23-brook-launch-new-zealand

the "23" is the article id and the "brook-launch-new-zealand" is the article title.

When I do:

echo $_GET['title'];

it only echos back: "brook" not the full string.

I am using mod_rewrite for my urls, code follows:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

How can I echo the full string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

王权女流氓 2024-12-16 14:57:45

您没有匹配可能的 -

... ([0-9]+)-([a-zA-Z\-]+) /news/...
                     ^^ You need to add these

因此完整的行将是:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z\-]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

You aren't matching against possible -s:

... ([0-9]+)-([a-zA-Z\-]+) /news/...
                     ^^ You need to add these

So the full line would be:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z\-]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]
司马昭之心 2024-12-16 14:57:45

您的重写规则不支持破折号,请尝试以下一个:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([^/]*) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

Your rewrite rule does not support dashes try this one:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([^/]*) /news/view-article.php?category=$1&id=$2&title=$3 [NC]
手心的温暖 2024-12-16 14:57:45

您用于匹配标题 ([a-zA-Z]+) 的正则表达式仅匹配一组字母 (az),不包含破折号 (-) 符号。也许正则表达式的那部分应该是: (([a-zA-Z]+-?)+)

那么你的重写规则将是:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-(([a-zA-Z]+-?)+) /news/view-article.php?category=$1&id=$2&title=$4 [NC]

The regex that you are using to match the title ([a-zA-Z]+) only matches a set of leters (a-z) and does not include a dash (-) symbol. Perhaps that part of the regular expression should be: (([a-zA-Z]+-?)+)

Then your rewrite rule would be:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-(([a-zA-Z]+-?)+) /news/view-article.php?category=$1&id=$2&title=$4 [NC]
尐籹人 2024-12-16 14:57:45

我想你可能想要:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z0-9-]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

在你的版本中,标题中只允许使用字母 (a-zA-Z),因此正则表达式与破折号不匹配。上面的内容应该与破折号相匹配,并且还允许标题中出现数字(我怀疑您会希望允许)。

您甚至可能只想这样做:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-(.+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

...并允许标题中包含任何字符 - 您可以轻松摆脱这一点,因为标题是 URL 的最后部分。

I think you probably want:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-([a-zA-Z0-9-]+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

With your version, only the letters (a-zA-Z) are allowed in a title, so the regex doesn't match the dashes. The above should match the dashes, and also will allow numbers in the title (which I suspect you will want to allow).

You may even simply want to do:

RewriteRule ^news/categories/([a-zA-Z]+)/([0-9]+)-(.+) /news/view-article.php?category=$1&id=$2&title=$3 [NC]

...and allow any character in the title - which you can easily get away with, since the title is the last portion of the URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文