我有一个子目录/categories/stories/
我想重写/books/
但我不想重写类似/categories/stories/horror
的东西。我正在执行以下操作,但它没有效果:
RewriteCond ${REQUEST_URI} ^/categories/stories[/]?
RewriteCond ${REQUEST_URI} !^/categories/stories/+[/]?
RewriteRule ^(.*) /books/ [R=301,L]
结果是,例如,如果一个人去了,http://bookstore.com/categories/stories
他们去了http://bookstore.com/books
但http://bookstore.com/categories/stories/horror
没有重写。
您可以在正则表达式模式中使用锚点:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/categories/stories/?$ [NC]
RewriteRule ^ /books/ [R=301,L]
模式^/categories/stories/?$
将匹配/categories/stories/
或/categories/stories
,但不匹配/categories/stories/anything
。
此外,请确保清除浏览器缓存或使用其他浏览器来测试此更改。