【问题标题】:.Htaccess URL Rewriting - Clean URLs not working using .htaccess.Htaccess URL 重写 - 清理 URL 无法使用 .htaccess
【发布时间】:2022-01-04 00:31:55
【问题描述】:

您好,我想将下面的 URL https://example.com/item?id=223 改写为 https://example.com/google/223。这里会动态生成ID

这个我试过了,但是不行

RewriteRule ^google/$ item?id=$1    [R=301,L] 

任何帮助将不胜感激?

【问题讨论】:

  • 感谢分享您的努力,请确认您是否在浏览器中点击链接https://example.com/item?id=223
  • example.com 只是为了理解。实际的 URL 是另一回事。是否需要分享实际的url?
  • 是的,我明白了。我的意思是:你的网址中是否有:item?id=223
  • 是的,我想要的是如果有人写example.com/item?id=223 url 应该重写为example.com/google/223example.com/item?id=223 相同的内容
  • 好的,所以您想将浏览器中的 url 更改为 google/223 并在后端由 index.php 文件或其他文件提供服务?

标签: apache .htaccess mod-rewrite friendly-url


【解决方案1】:

根据您显示的尝试,请尝试遵循 htaccess 规则。请确保您的 htaccess 规则文件和 index.php 文件存在于同一文件夹中。

请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##External redirect in browser to new URL.
RewriteCond %{THE_REQUEST} \s/item\?id=(\d+)\s [NC]
RewriteRule ^ /google/%1? [R=301,L]

##Internal rewrite to index.php in backend server.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^google/(\d+)/?$ index.php?id=$1 [QSA,NC,L]

【讨论】:

  • 好的。谢谢你。如果它由 item.php 提供服务怎么办
  • @Pardeep,然后将上面代码中的index.php 替换为item.php,然后告诉我进展如何?
  • 我把 index.php 改成了 item.php 还是不行。
  • @Pardeep,您点击的是什么示例网址?你遇到了什么错误? item.php 和 htaccess 是否存在于同一位置?
  • 谢谢。它奏效了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
相关资源
最近更新 更多