简体   繁体   English

.htaccess将所有子文件夹内容(带有和不带有斜杠)重定向到同一个子文件夹

[英].htaccess redirect all subfolder content (with and without ending slash) to same subfolder

I want to redirect the following URLs: 我想重定向以下网址:

/gallery/image-1/
/gallery/image-1
/gallery/image-2/
/gallery/image-2

to /gallery/ , but it's harder than what it at first seems. to /gallery/ ,但它比起初看起来更难。 This is from the .htaccess: 这是来自.htaccess:

RewriteEngine On
RewriteRule ^gallery/(.*) /gallery/ [R=301,L,NC]

All URLs are initially redirected to /gallery/ , but then it just keeps loading, as apparently this URL also goes under ^gallery/(.*) , so it keeps redirecting to the same URL. 所有网址最初都会重定向到/gallery/ ,但之后只会继续加载,因为显然此网址也位于^gallery/(.*) ,因此它会一直重定向到相同的网址。 After a few seconds, the browser gives me a ERR_TOO_MANY_REDIRECTS message. 几秒钟后,浏览器会给我一条ERR_TOO_MANY_REDIRECTS消息。

How can I edit the ^gallery/(.*) statement to only cover URLs with something after the /gallery/ URL? 如何编辑^gallery/(.*)语句以仅覆盖/gallery/ URL之后的某些URL?

You should be using .+ instead of .* in your regex to make sure to stop redirecting when URI is /gallery/ : 您应该在正则表达式中使用.+而不是.*以确保在URI为/gallery/时停止重定向:

RewriteEngine On

RewriteRule ^(gallery)/.+$ /$1/ [R=301,L,NC]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM