简体   繁体   中英

Using .htaccess to redirect all sub-folder's directory to Root (main) Directory

Let say my domain is http://example.com/ , and I have a page such as http://example.com/abc/ (Used rewrite rule to remove the extension of the page file).

But sometimes, user type http://example.com/abc/xxx/ (xxx is a various, so many chance they type) such as http://example.com/abc/1njnsdc , http://example.com/abc/diief123/ , etc. Normally, it will make request to server to need /abc/xxx/ , /abc/injnsdc/ , /abc/diief123/ etc. Unluckily, we don't have files inside the folder /abc/ so the server should be return 404 error, not found.

I hope to make a rewrite rule to make server get request like http://example.com/abc/xxx/ , http://example.com/abc/1njnsdc/ , http://example.com/abc/diief123/ will not return 404 but respond redirect to http://example.com/abc/ . Another static request from me is the URL cannot change. So that once the user type http://example.com/abc/xxx/ , http://example.com/abc/1njnsdc/ , http://example.com/abc/diief123/ etc, all of that will can access to what user type http://example.com/abc/???/ . And user is still reading the page http://example.com/abc/ .

you may try to redirect the 404 page to the main page! if you don't want it to show at all. if you are using php this could be useful for you:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>

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