简体   繁体   中英

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.

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):

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. 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

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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