简体   繁体   中英

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.

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

I want the URL to be: 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]

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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