大网数据广告活动
网站运营与故障问答

Forcing trailing slash at end of "fake" folders

· 52CE 我爱测

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 wi…

问题原文

原帖链接 · 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]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-04-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。