【问题标题】:Rewrite url with 2 parameters in htaccess在 htaccess 中用 2 个参数重写 url
【发布时间】:2013-12-18 13:20:57
【问题描述】:
example.com/index.php?route=pavblog/blogs&id=$2

我需要将此网址加载到

example.com/blogs/blogname

我在 .htaccess 中尝试过这样的:

RewriteRule ^blogs/(.*) index.php?route=pavblog/blogs&id=$2

但它不起作用。

【问题讨论】:

  • 为什么 2 在 $2 ?为什么不是 1 美元?

标签: php apache


【解决方案1】:

我想你想要这个:

RewriteRule ^blogs/(.*) index.php?route=pavblog%2Fblogs&id=$1

我将$2 更改为$1(只有一个被捕获的组)并且我将/ 转义为%2F

【讨论】:

  • 嗨 Dystroy,非常感谢您的回答,但我需要加载这个 url example.com/index.php?route=pavblog/blogs 就像这个 example.com/blogs/ 和这个 url example.com/index.php?route=pavblog/blogs&id=13 就像这个 example.com/blogs/blogname
  • 试试/index.php 而不仅仅是index.php。有时这会有所帮助。此外,如果原始传入 URI 中有任何查询字符串值,则需要一个 [QSA] 标志来保留它们。最重要的是,保持您的术语直截了当:您的传入(来自外部世界)URI 是 /blogs/blogname,并且您正在将其转换为服务器和 PHP 可以理解的形式:/index.php?.... 现在,如果博客 id 可以为空(null),您希望发生什么?
  • 你希望如何处理 /blogs/ ,而在第二种情况下,你不能这样做——它没有地方从传入的 URI 中获取 id。您可以将一个博客与另一个博客区分开来的只有博客名称。如果博客名称包含 id(例如,“myblog-13”或“13/myblog”),这将是可能的。
  • 非常感谢您的建议,我已经在 url 上添加了带有 id 的博客名称并修复了问题。
猜你喜欢
  • 2016-05-20
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多