问题原文
原帖链接 · Jefferson · CC BY-SA 3.0
The working codes that I've found here most put a slash in everything.
I just need force trailing slash in these "fake dirs" (that are part of my rewriting scheme (only directories)) and keep everything else (fake files ending with .html) as it is.
Here is my .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com [nc]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
DirectoryIndex index.php?category=1
Options -Indexes
RewriteRule ^album/([0-9]+)$ /index.php?p=$1&opc=4
RewriteRule ^album/$ /index.php?p=1&opc=4
RewriteRule ^album/(.*)-(.*).html /view.php?id=$2&lang=$1
RewriteRule ^search/(.*)/$ /index.php?lang=en&search=$1 [L,QSA]
RewriteRule ^channels/(.*)/$ /index.php?lang=en&channel=$1 [L,QSA]
RewriteRule ^(.*)/favorites/$ /index.php?lang=en&favorites=$1 [L,QSA]
RewriteRule ^(.*)/likes/$ /index.php?lang=en&likes=$1 [L,QSA]
RewriteRule ^(.*)/albums/(.*)/$ /index.php?lang=en&albumid=$2|$1 [L,QSA]
RewriteRule ^(.*)/albums/$ /index.php?lang=en&album=$1 [L,QSA]
RewriteRule ^albums/$ /index.php?lang=en&album=all [L,QSA]
RewriteRule ^newest/$ /index.php?opc=1&lang=en [L,QSA]
RewriteRule ^popular/$ /index.php?opc=2&lang=en [L,QSA]
RewriteRule ^best/$ /index.php?opc=3&lang=en [L,QSA]
RewriteRule ^categories/$ /index.php?category=1 [L,QSA]
RewriteRule ^categories/([0-9]+)$ /index.php?p=$1&category=1 [L,QSA]
RewriteRule ^categories/(.*)/$ /index.php?lang=en&categories=$1 [L,QSA]
RewriteRule ^categories/(.*)/([0-9]+)$ /index.php?lang=en&categories=$1&p=$2 [L,QSA]
已采纳解答
原帖链接 · Stephen Ostermiller · CC BY-SA 3.0
You will need to redirect to add the slash. This will redirect any set of letters without other punctuation such as slashes or dots in them. I don't think it conflicts with any of your other rewrite rules.
RewriteRule ^((.*/)?[a-z]+)$ http://www.example.com/$1/ [R=301,L]
You could make it a bit safer by making sure it doesn't match existing files on disk. To do so prepend these two conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Alternately, you could put in separate rules for each of your "fake" directories:
RewriteRule ^(album|categories|newest|best)$ /$1/ [R=301,L]
RewriteRule ^((search|categories)/[^/]+)$ /$1/ [R=301,L]
RewriteRule ^(.*/(favorites|likes|albums))$ /$1/ [R=301,L]
RewriteRule ^(.*/albums/[^/]+)$ /$1/ [R=301,L]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
