【问题标题】:.htaccess - Check if multiple files exists based on URL slug and rewrite accordingly?.htaccess - 根据 URL slug 检查是否存在多个文件并相应地重写?
【发布时间】:2014-02-01 09:56:57
【问题描述】:

来自以下网址:

localhost/videos/the-best-games

还有一个名为“categories”的目录,其中包含与 .htaccess 相同的目录中的各种文件:

./games.txt
./sports.txt
./football.txt

如何使用 url slug "the-best-games" 来检查 "categories" 中的这些文件名

./categories/the.txt
./categories/best.txt
./categories/games.txt

并使用第一个匹配的文件名重写 url:

localhost/games/videos/the-best-games

localhost/games/videos/the-best

可能基于这些先前的问题:

【问题讨论】:

  • URL 可以是无限长的,例如:http://localhost/videos/the-best-games-on-earth,并且您还想检查on.phpearth.php 吗?
  • 这很好,但我们可以慢慢开始。

标签: apache .htaccess mod-rewrite slug backreference


【解决方案1】:

这应该适合你:

RewriteCond /path/to/categories/$1.txt -f
RewriteRule /videos/the-best-(.*)$ /$1/videos/the-best-$1 [R,L]

它会解析规则并寻找最佳的(无论如何),获取该值并检查文件系统中位于您的类别的绝对路径的文件。然后它将进行重写。 如果要从重写 URL 的末尾删除类别:

RewriteCond /path/to/categories/$1.txt -f
RewriteRule /videos/the-best-(.*)$ /$1/videos/the-best [R,L]

【讨论】:

  • 谢谢,这是一个好的开始,但是如何检查.txt 和 best.txt 是否存在?
猜你喜欢
  • 1970-01-01
  • 2011-09-11
  • 2015-05-21
  • 2015-07-28
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
相关资源
最近更新 更多