htaccess重写规则,以从URL中删除多个子文件夹 [英] htaccess rewrite rules to remove multiple subfolders from a URL

查看:93
本文介绍了htaccess重写规则,以从URL中删除多个子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很可能会遇到文件系统子目录约束,我想将/users文件夹分为/users/a/username/users/b/username/users/c/username等.

Due file system sub-directory constraints I will most likely reach I want to separate the /users folder into /users/a/username, /users/b/username, /users/c/username, etc.

因此,用户在服务器上的数据将位于: www.domain.com/users/a/username/www.domain.com/users/b/username/等

So the data on the server for a user would be in: www.domain.com/users/a/username/, www.domain.com/users/b/username/, etc

我希望URL为: www.domain.com/users/username/

为了避免重复的内容,可以选择将301从www.domain.com/users/a/username/重定向到www.domain.com/users/username/也很好.

Optionally to avoid duplicate content a 301 redirect from www.domain.com/users/a/username/ to www.domain.com/users/username/ would also be good.

目前,我对单个子目录进行了重写(请参见下文),但是我对如何有效地对所有字母顺序子目录进行混淆感到困惑.

Currently I have a rewrite for a single sub-directory (see below) but I'm confused how this can be done efficiently for all of the alphabetical sub-directories.

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^users/(.*)$ users/a/$1 [L,NC]

我已经检查了这个网站,所有人似乎都隐藏了第一个子目录,例如domain.com/folder1/filename.html => domain.com/filename.html,但没有更深入的说明.

I have checked this site and all seem to hide the first sub-directory e.g. domain.com/folder1/filename.html => domain.com/filename.html but nothing in more depth.

推荐答案

将此代码放在htaccess中(该文件必须位于根文件夹中)

Put this code in your htaccess (which has to be in root folder)

Options -Indexes -MultiViews

RewriteMap lowercase int:tolower    # this line in your apache file configuration
RewriteEngine On

RewriteCond %{THE_REQUEST} \s/users/[a-z]/([^/\s]+)\s
RewriteRule . /users/%1/? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^users/([A-Za-z])([^/]+)/$ /users/${lowercase:$1}/$1$2/ [L]

这篇关于htaccess重写规则,以从URL中删除多个子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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