移除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} !-dline 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 forstatic/foo.htmlinstead ofstatic/foo/.html.
内容的提问来源于stack exchange,提问作者arjun

