【问题标题】:htaccess mod rewriting url displays 404htaccess mod 重写 url 显示 404
【发布时间】:2013-08-03 04:38:48
【问题描述】:
#Create friendly URL
RewriteRule ^$1-$2.php?$3=$4$ (.*)/(.*)/(.*)/(.*)/[L]

你好,stackoverflow!

我网站的当前站点(我们称之为 site.com)有这样的设置:

http://site.com/category/page/variable1/value1/variable2/value2/
http://site.com/category/page/variable1/value1/
http://site.com/category/page/
http://site.com/

我想将这些网址重写为:

http://site.com/category-page.php?variable1=value1&variable2=value2
http://site.com/category-page.php?variable1=value1
http://site.com/category-page.php
http://site.com/

上面的代码是我尝试编写的。它没有给我服务器错误消息,而是显然不起作用 - 当我转到 http://site.com/category/page/variable1/value1/variable2/value2/ 时,它只会给出 404 错误。

你们能帮帮我吗?

提前致谢

【问题讨论】:

  • Regex 和替换在您的 RewriteRule 中切换。您还提供了一个包含六个路径段的示例,但您的规则只允许四个。你检查过stackoverflow.com/tags/mod-rewrite/info 标签维基中的例子了吗?
  • @mario 这是我设置的第二个示例,因为我只是在测试初始重写。
  • @hungerstar 并不是说​​这个答案无关紧要,因为categorypage 等有数百个不同的名称,手动将它们添加到 .htaccess 文件并不方便。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

我认为你的正则表达式和替换是倒退的。下面的 sn-p 只是计算/捕获斜线之间的内容以进行重写。

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /$1-$2.php?$3=$4&$5=$6 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) /$1-$2.php?$3=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ $1-$2.php [L]

【讨论】:

  • 我什至无法访问根目录中的 index.php 文件。我该如何解决?
  • 已解决,与另一个 htaccess 文件有关。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多