You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用.htaccess为多个指定URL移除尾部斜杠?

Adjust .htaccess for Multiple Specific URLs (Remove Trailing Slash) + Add Slash to Others

No problem, let's tweak the original .htaccess code to handle multiple specific paths that need their trailing slashes removed, while keeping the rule to add trailing slashes to all other non-file pages. Here's the updated solution:

RewriteEngine on

# Remove trailing slash for specified URLs (demo, demo2, demo3, demo4)
RewriteCond %{REQUEST_URI} ^/(demo|demo2|demo3|demo4)/$ [NC]
RewriteRule ^(demo|demo2|demo3|demo4)/$ /$1 [L,R=301]

# Add trailing slash to all other non-file URLs
RewriteCond %{REQUEST_URI} !^/(demo|demo2|demo3|demo4) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]

Quick breakdown of the changes:

  • First rule block: We use a regex group (demo|demo2|demo3|demo4) to match all your specified paths. This way, any request like abc.com/demo2/ will redirect to abc.com/demo2, same for demo3, demo4, etc. The [NC] flag makes the match case-insensitive, and R=301 is a permanent redirect (use R=302 temporarily if you're testing changes).
  • Second rule block: We updated the exclusion condition to skip all your specified paths (!^/(demo|demo2|demo3|demo4)), so the "add trailing slash" rule only applies to every other page that isn't a file (!-f ensures we don't add slashes to actual files like images or CSS).

Pro tip:

If you need to add more URLs later (like demo5, demo6), just extend the regex group with a pipe separator, e.g., (demo|demo2|demo3|demo4|demo5).

内容的提问来源于stack exchange,提问作者indy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:40:42