【发布时间】:2014-11-12 06:25:59
【问题描述】:
我不知道如何命名这个问题,但这是我的问题。
我正在尝试将多个规则转换为一个指向一页的规则。
该网站有一个照片目录,最好使用以下url结构访问,并且图像编号必须以零开头:
(插入基本网址)/photocategoryname/month-01-2014/image/1
我希望 (insert base url)/photocategoryname/month-01-2014/image/0 自动切换到 (insert base url)/photocategoryname/month-01-2014/image/1 并从中加载数据
我也希望 (insert base url)/photocategoryname/month-01-2014/image-1 转换为 (insert base url)/photocategoryname/month-01-2014/image/1 因为我做了一个 URL重新设计,过去我使用 image-1 而不是 image/1,搜索引擎正在尝试检索旧文件。
我还想处理意外添加额外斜线的情况。
当我使用此规则集并尝试访问 (insert base url)/photocategoryname/month-01-2014/image-1 时,它给了我一个重定向页面,新 URL 为 (insert base url)/photocategoryname/month- 01-2014/image/1/month-01-2014/image-1.
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/image-([0-9]+)(/)?$ /$1/$2-$3-$4/image/$5 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/([0-9]+)(/)?$ /$1/$2-$3-$4/image/$5 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/([0-9]+)/(.+)$ /$1/$2-$3-$4/image/$5 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/image/0(/)?$ /$1/$2-$3-$4/image/1 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/image/([0-9]+)$ /photo.php?TITLE=$1&DATE=$2-$3-$4&PHOTONUMBER=$5 [NC,L]
我想要做的是将重定向最小化为 1。在前 4 条规则使用 [R=301,NC,L] 而不是 [NC] 之前,我能够使此设置正常工作,但这导致重定向太多,我不希望我的服务器花费资源来处理不必要的重定向。
知道我做错了什么吗?
更新:
这就是我想要实现的目标。
我希望允许人们使用以下任一 URL 访问同一页面(照片):
(insert base url)/photocategoryname/month-##-####/image#
(insert base url)/photocategoryname/month-##-####/image/#
(insert base url)/photocategoryname/month-##-####/image-#
(insert base url)/photocategoryname/month-##-####/photo#
(insert base url)/photocategoryname/month-##-####/photo/#
(insert base url)/photocategoryname/month-##-####/photo-#
(insert base url)/photocategoryname/month-##-####/picture#
(insert base url)/photocategoryname/month-##-####/picture/#
(insert base url)/photocategoryname/month-##-####/picture-#
我编写了 PHP 代码,可以帮助将用户重定向到正确的 URL:
(insert base url)/photocategoryname/month-##-####/image/#
这个旧代码作为重写规则的第二个参数有效,但仅适用于一个 URL。
^(.+)/(.+)-([0-9]+)-([0-9]+)/image/([0-9]+)$
但是这个似乎在服务器上完全被忽略了:
^([^/]+)/([A-Z0-9]+)-([0-9]+)-([0-9]+)/(image|photo|picture|photograph|pic|)(-|/|)([0-9]+)/*$
两行代码都以 [NC,L] 结尾。
我做错了什么?
【问题讨论】:
标签: apache .htaccess mod-rewrite