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

移除URL末尾斜杠后执行RewriteCond规则的.htaccess配置求助

Got it, let's sort out this issue where URLs ending with a slash aren't matching your static HTML files properly. The problem is straightforward: when someone visits www.website/foo/, the REQUEST_URI becomes /foo/, so your current rule looks for static/foo/.html—which obviously doesn't exist.

Here's how to adjust your .htaccess to first strip that trailing slash, then check for the corresponding HTML file:

Option 1: Permanent Redirect (SEO-Friendly)

This will update the user's browser URL to remove the slash (e.g., www.website/foo/ → www.website/foo) and then serve the matching HTML file.

<IfModule mod_rewrite.c>
    RewriteEngine on

    # Strip trailing slash (skip actual directories to avoid breaking them)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ $1 [L,R=301]

    # Original rule: check if the HTML file exists in static/ and serve it
    RewriteCond "%{DOCUMENT_ROOT}/static/%{REQUEST_URI}.html" -f
    RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/static/$1.html" [L]
</IfModule>

Option 2: Internal Rewrite (Keep URL As-Is)

If you don't want the user's URL to change (they'll still see /foo/ in the address bar), use an internal rewrite instead of a redirect:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # Internally strip trailing slash (no browser redirect)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ $1 [L]

    # Original rule now works with the stripped URI
    RewriteCond "%{DOCUMENT_ROOT}/static/%{REQUEST_URI}.html" -f
    RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/static/$1.html" [L]
</IfModule>

Key Notes:

  • The RewriteCond %{REQUEST_FILENAME} !-d line ensures we don't interfere with actual directories on your server—their trailing slashes will stay intact.
  • In both setups, the trailing slash gets removed before we check for the HTML file, so www.website/foo/ will now correctly look for static/foo.html instead of static/foo/.html.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:19:18