重写规则从路径中删除一个文件夹,并保持目录结构的其他部分 [英] rewrite rule to remove a folder from a path and keep rest of directory structure

查看:249
本文介绍了重写规则从路径中删除一个文件夹,并保持目录结构的其他部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的根目录我有很多文件夹。我希望把这些文件夹到一个子目录。

In my base directory I have a lot of folders. I would like to put these folders into a sub-directory.

domain.com/test/test.html

domain.com/test/test.html

将具有所述文件结构:

domain.com/subdir/test/test.html

domain.com/subdir/test/test.html

和仍然在同一网址:domain.com/test /

and still be at the same url: domain.com/test/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

从这里另一个答案抄做的,但它也将停止其他请求的基本目录。

Copied from another answer here does that but it also stops other requests to the base directory.

我需要它来检查文件夹名的子目录匹配,如果它存在,或如果不从基本目录服务。

I need it to check for foldername matches in the subdir and if it exists or if not serve from base directory.

推荐答案

试试这个:

RewriteEngine on

# these conditions are optional, they're to make sure you don't clobber a legit request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# these conditions aren't optional, they check to see if the requested file/dir exists inside /subdir
RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -d

# rewrite
RewriteRule ^(.*)$ /subdir/$1 [L]

这篇关于重写规则从路径中删除一个文件夹,并保持目录结构的其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆