大网数据广告活动
服务器运维问答

Unsure what this .htaccess set of rules is doing

· 52CE 我爱测

I've inherited a web application, and in it is the following set of .htaccess rules. It appears to me that they do nothing. Can someone explain what they're doing? To me, it looks like they're matching only hits to the site root,…

问题原文

原帖链接 · e_i_pi · CC BY-SA 3.0

I've inherited a web application, and in it is the following set of .htaccess rules. It appears to me that they do nothing. Can someone explain what they're doing?

To me, it looks like they're matching only hits to the site root, checking if the directory doesn't exist (!), and then 301 redirecting to the site root.

RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Just to add to OverCoder's answer... as already stated, this code removes the slash at the end of the URL (except for directories) ...

RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

However, the first RewriteCond directive here is indeed superfluous and can be removed. This simply checks that the URL ends in a slash, which is what the RewriteRule pattern (ie. ^(.+)/$) has already established.

And the second RewriteCond directive makes sure we are not requesting a directory. A directory requires a trailing slash. By default, mod_dir will append a trailing slash if you request a directory without. So, if you then removed the trailing slash on directories with mod_rewrite then you'd likely create a redirect loop.

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

资料来源与适用范围

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

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

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