使用.htaccess将子目录重写为root,但保持对其他子目录的访问(Use .htaccess to rewrite a sub directory to root but keep access to other sub directories)
我试图从我的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]我错过了什么? 什么样的规则组合将帮助我实现我正在寻找的东西?
I am trying to rewrite a subdirectory out of my URL structure but have other URLs within that directory structure also work. I have this scenario:
// 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 onHere's where i'm at. I can't get the right combination of htaccess rules to achieve what I want.
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]What am I missing? What combination of rules will help me achieve what i'm looking for?
最满意答案
您可以使用正则表达式匹配将重定向规则合并为单个规则:
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]Your redirect rules can be combined into single rule using regex matching:
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]更多推荐
发布评论