简体   繁体   中英

.htaccess rewrite subfolders to remove /index

I have some htaccess rules which rewrite friendly URLs. Everything is working apart from one thing.

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

This is a site with language subfolders, eg:

/fr/about.html

/en/about.html

With the above htaccess rules these urls come out like:

/fr/about

/en/about

However this also means my index pages URLs look like:

/fr/index

/en/index

What I would like, is for /index the URL looks like

/fr

/en

Which would mean they respect the trailing slash part of my above htaccess but remove index from the URL.

Any help much appreciated!

Try to add (before RewriteCond %{THE_REQUEST} ^ ...):

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*?)/index(\.html)?[\s?] [NC]
RewriteRule ^ %1 [R=301,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