简体   繁体   English

如何通过 .htaccess 将包含 2 个正斜杠的任何 URL 重定向到主页?

[英]How to redirect any URL that contains 2 forward slashes to the homepage via .htaccess?

I need to redirect any URL that contains 2 or more forward slashes in a row back to the homepage.我需要将任何连续包含 2 个或更多正斜杠的 URL 重定向回主页。

I have tried:我努力了:

RewriteRule example.com(.*)// https://example.com [R,L]

But it does not work and I don't understand why as it is pretty straightforward.但它不起作用,我不明白为什么,因为它非常简单。

How can I do this?我怎样才能做到这一点?

Here is how you can achieve this (assuming /home is the path to your homepage. remove it in the RewriteRule if domain root is your homepage):以下是实现此目的的方法(假设/home是您主页的路径。如果域根目录是您的主页,则在RewriteRule中将其删除):

RewriteEngine On
RewriteCond %{REQUEST_URI} //
RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/home [R,L]

Explanation解释

RewriteCond %{REQUEST_URI} Condition is met if request uri contains a double slash. RewriteCond %{REQUEST_URI}如果请求 uri 包含双斜杠,则满足条件。 The slash does not need to be escaped (preceeded with \ ) as it carries no special meaning in the regex斜杠不需要转义(以\开头),因为它在正则表达式中没有特殊含义

RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/home Rewrite the url and redirect to http(s)://yourdomain/home RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/home重写 url 并重定向到http(s)://yourdomain/home

Demo 演示

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

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