我正在尝试从我的URL结构中重写一个子目录,但该目录结构中的其他URL也可以工作。我有这种情况:
// This should load the content of mysite.com/foo/15 but not redirect
mysite.com/foo/
// Subdirectories within /15 should also work
mysite.com/foo/bar // the actual location of this is mysite.com/foo/15/bar
// Other URLs within this directory structure should also work, such as
mysite.com/foo/12
mysite.com/foo/13
mysite.com/foo/14
// Lastly, we have redirects from 2012 -> 12, 2013 -> 13 such that
mysite.com/foo/2012 becomes mysite.com/foo/12
mysite.com/foo/2013 becomes mysite.com/foo/13
... and so on
我在这里的地方。我无法获得正确的htaccess规则组合来实现我想要的目标。
RewriteEngine On
RewriteBase /foo/
// This makes mysite.com/foo/ load the content of mysite.com/foo/15 without redirecting, but makes mysite.com/foo/14 404
RewriteRule ^((?!15/).*)$ 15/$1 [NC,L]
// This works pretty well, except the rules below don't redirect
RewriteRule ^$ 15/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 15/$1
// Rewrite old years to the two digit form
RewriteRule ^2010/(.*)$ 10/$1 [R=301]
RewriteRule ^2011/(.*)$ 11/$1 [R=301]
RewriteRule ^2012/(.*)$ 12/$1 [R=301]
RewriteRule ^2013/(.*)$ 13/$1 [R=301]
RewriteRule ^2014/(.*)$ 14/$1 [R=301]
RewriteRule ^2015/(.*)$ 15/$1 [R=301]
我错过了什么?什么样的规则组合将帮助我实现我所寻找的目标?
答案 0 :(得分:1)
尝试在所有规则中加入L
标记,然后将所有重定向放在htaccess文件的顶部:
RewriteEngine On
RewriteBase /foo/
// Rewrite old years to the two digit form
RewriteRule ^2010/(.*)$ 10/$1 [R=301,L]
RewriteRule ^2011/(.*)$ 11/$1 [R=301,L]
RewriteRule ^2012/(.*)$ 12/$1 [R=301,L]
RewriteRule ^2013/(.*)$ 13/$1 [R=301,L]
RewriteRule ^2014/(.*)$ 14/$1 [R=301,L]
RewriteRule ^2015/(.*)$ 15/$1 [R=301,L]
// This makes mysite.com/foo/ load the content of mysite.com/foo/15 without redirecting, but makes mysite.com/foo/14 404
RewriteRule ^((?!15/).*)$ 15/$1 [NC,L]
// This works pretty well, except the rules below don't redirect
RewriteRule ^$ 15/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 15/$1 [L]
答案 1 :(得分:1)
您可以使用正则表达式匹配将重定向规则合并为单个规则:
RewriteEngine On
RewriteBase /foo/
// Rewrite old years to the two digit form
RewriteRule ^20(1[0-5])/(.*)$ $1/$2 [R=301,L]
RewriteRule ^$ 15/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!15/).*)$ 15/$1 [L]