如何使用.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 likeabc.com/demo2/will redirect toabc.com/demo2, same for demo3, demo4, etc. The[NC]flag makes the match case-insensitive, andR=301is a permanent redirect (useR=302temporarily 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 (!-fensures 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
相关产品推荐
相关产品推荐

