.htaccess-将多个子目录重写为root [英] .htaccess - Rewrite multiple subdirectories to root

查看:51
本文介绍了.htaccess-将多个子目录重写为root的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个子目录重写到根目录.我遇到的情况是,我有一个名为 blog/的文件夹,其中包含主站点文件夹,还有另一个名为 projects/的子目录,该子目录包含我想要从其访问的其他文件夹.根:

I am trying to rewrite multiple subdirectories to the root directory. The situation I have it that I have one folder named blog/ which would contain the main site folder and also another subdirectory called projects/ containing other folders which I want accessible from the root:

www/
    blog/
        work/
        contact/
    projects/
        projectA/
        projectB/

我想要的是能够访问 work/ contact/ projectA/ projectB/ example.com/projectA example.com/projectB .我还希望 blog/目录在两个文件夹都存在的情况下具有优先权.

What I want, is to be able to access work/, contact/, projectA/ and projectB/ from the root directory by going to example.com/projectA or example.com/projectB for example. I would also like the blog/ directory to take priority in the case that a folder exists in both.

目前,在我的htaccess中,我有这个功能,尽管它只是将所有内容重写为 blog/,并从URL中删除了它,尽管并没有影响 projects/文件夹.

Currently, in my htaccess, I have this, though it just rewrites everything to blog/, removing this from the URL, though not affecting the projects/ folder.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)site
RewriteRule ^(.*)$ site/$1 

推荐答案

尝试:

RewriteEngine on

# first check if request is in /blog/
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -d
RewriteRule ^(.*)$ /blog/$1 [L]

# then check if request is in /projects/
RewriteCond %{DOCUMENT_ROOT}/projects%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/projects%{REQUEST_URI} -d
RewriteRule ^(.*)$ /projects/$1 [L]

# otherwise, blindly rewrite to blog (or do nothing by removing this rule to allow a 404 not found)
RewriteCond ${REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L]

这篇关于.htaccess-将多个子目录重写为root的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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