简体   繁体   English

htaccess重写规则,以从URL中删除多个子文件夹

[英]htaccess rewrite rules to remove multiple subfolders from a URL

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. 由于文件系统子目录的限制,我很可能会想将/users文件夹分为/users/a/username/users/b/username/users/c/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 所以对于一个用户在服务器上的数据将是: www.domain.com/users/a/username/www.domain.com/users/b/username /等

I want the URL to be: www.domain.com/users/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. (可选)为避免重复的内容,从www.domain.com/users/a/username/到www.domain.com/users/username/的301重定向也不错。

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]

I have checked this site and all seem to hide the first sub-directory eg domain.com/folder1/filename.html => domain.com/filename.html but nothing in more depth. 我已经检查了这个站点,似乎都隐藏了第一个子目录,例如domain.com/folder1/filename.html => domain.com/filename.html但没有更深入的介绍。

Put this code in your htaccess (which has to be in root folder) 将此代码放在您的htaccess中(必须在根文件夹中)

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM