【问题标题】:how to do url rewriting for dynamically generated links如何为动态生成的链接进行 url 重写
【发布时间】:2015-10-02 05:17:52
【问题描述】:

我创建了一个动态生成的小链接。当我单击该链接时,它将创建一个这样的 URL

http://localhost/store/user.php?id=12

如果我给这个网址

http://localhost/store/user/12

这将产生我想看到的相同输出

我想要做的是,如果我点击应该产生的动态生成的链接

 http://localhost/store/user/12 

这个网址而不是这个

http://localhost/store/user.php?id=12

我该怎么做

.httaccess 文件是这个

# Enable Rewriting
RewriteEngine on

# Rewrite user URLs
#   Input:  user/NAME/
#   Output: user.php?id=NAME
RewriteRule ^user/(\w+)/?$ user.php?id=$1

【问题讨论】:

  • 我认为你有一个基本的误解,认为这个 .htaccess 代码会自动转换你的链接。事实上它不会。 .htaccess 代码所做的,更准确地说是 mod_rewrite 模块代码,是重新解释 URL 中的某些模式——我们喜欢称之为“漂亮的 URL”——它处理这些漂亮的 URL 并在内部重写它们。问题是,您必须以某种方式强制您的链接采用这种形式。无论是手动还是通过一些编程。
  • 我的意图是,当用户单击动态生成的链接时,我想生成一个类似 localhost/store/user/12 的链接 localhost/store/user.php?id=12,我该如何实现这一点?@MarkSkayff跨度>
  • 你说链接是动态生成的。这意味着某些东西正在生成它们。您必须能够修改某些东西,可能是脚本代码,并使其生成新的链接。那部分与.htaccess 无关。您必须首先调整链接的生成方式。
  • @MarkSkayff 谢谢你,马克我误解了 url 重定向谢谢坦克了很多。

标签: php html .htaccess url


【解决方案1】:
RewriteRule ^store/user/(\w+)/?$ store/user.php?id=$1

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2015-03-01
  • 1970-01-01
  • 2020-07-13
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多