使用.htaccess将子目录重写为root,但保持对其他子目录的访问(Use .htaccess to rewrite a sub directory to root but keep access

编程入门 行业动态 更新时间:2024-10-12 01:23:25
使用.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 on

Here'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]

更多推荐

本文发布于:2023-07-19 06:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1174869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子目录   重写   root   htaccess   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!