在URL中删除// xx
我需要重定向所有类似的URL:
example.com/podcasts//rebt
要使
example.com/podcasts
我尝试调整此代码以进行两者,但我无法正常工作:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
I need to redirect all URLs like this:
example.com/podcasts//rebt
to
example.com/podcasts
I am trying to adjust this code to do both but I can't get it to work:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要删除url-path末尾的某物> (例如
/podcasts // rebt
to/podcasts
,请尝试一下,请尝试以下是root.htaccess
文件的顶部:the_request
服务器变量包含初始请求标头的第一行(例如get/podcasts/podcasts/rebt http/1.1
),当请求内部重写时不会更改(request> request_uri
) >在丢弃双斜线的第一个斜线之前,请捕获URL路径的
一部分>%1 BackReference包含捕获的子图案(即在URL path中的第一个双斜线之前的所有内容),从前一个 condpattern
一边:请注意如果前面的URL路径映射到物理目录,这将无法正常工作,因为它将导致两个重定向。例如。
/Directory //某物
to/Directory
to/Directory/code>(由MOD_DIR)。在这种情况下,您应该避免删除第一个尾随的斜线。
您应该首先使用302(临时)重定向测试,以避免任何潜在的缓存问题,并且在确定它按预期工作时仅更改为301(永久)重定向。在测试之前,您应该清除浏览器缓存。
看您现有的规则...
该代码旨在将多个斜线减少到URL路径中的单个斜线,而不是删除 完全是双斜线和剩余路径。例如。
/podcasts // rebt
/podcasts/rebt
。但是,由于它会检查request_uri
服务器变量(可以在整个请求中进行更改),因此可能无法按预期工作。另外,除非您错误地发布到内部双重斜线的URL,否则对
request_method
检查的条件似乎是多余的? 301重定向删除任何邮政数据(因为浏览器将其转换为获取) - 因此,为什么在某些情况下可能需要检查。To remove
//<something>
at the end of the URL-path (eg./podcasts//rebt
to/podcasts
, try the following instead at the top of the root.htaccess
file:THE_REQUEST
server variable contains the first line of the initial request headers (eg.GET /podcasts/rebt HTTP/1.1
) and does not change when the request is internally rewritten (unlikeREQUEST_URI
).The regex
\s([^?]+?)//
captures the part of the URL-path before the first instance of a double slash. Anything after and including the double slash, are discarded. This regex also ensures we do not inadvertently match against the query string (if any).The
%1
backreference contains the captured subpattern (ie. everything before the first double slash in the URL-path) from the preceding CondPattern.Aside: Note that this will not work properly if the preceding URL-path maps to a physical directory, since it will result in two redirects. eg.
/directory//something
to/directory
to/directory/
(by mod_dir). In this case, you should avoid removing the first trailing slash.You should test first with a 302 (temporary) redirect to avoid any potential caching issues and only change to a 301 (permanent) redirect when you are sure it's working as intended. You should clear your browser cache before testing.
A look at your existing rule...
This code is intended to reduce multiple slashes to single slashes in the URL-path, not remove the double slash and remaining path entirely. eg.
/podcasts//rebt
to/podcasts/rebt
. However, since it checks against theREQUEST_URI
server variable (which can change throughout the request) it may not work as intended.Also, the condition that checks against the
REQUEST_METHOD
would seem to be redundant, unless you are erroneously POSTing to double-slashed URLs internally? A 301 redirect removes any POST data (since the browser converts it to GET) - hence why the check may be necessary in certain cases.