简体   繁体   中英

hide and rename a folder using .htaccess file

Suppose, I have a link in my website as follows: www.example.com/test/index.php . I want to hide the 'test' folder from visitors and make it visible as 'apple' so that when the user types www.example.com/apple or www.example.com/apple/index.php , it automatically redirects to www.example.com/test/index.php . I want to make it possible by editing the .htaccess file as follows:

RewriteEngine On
RewriteRule ^test/(.*) apple/$1

But, it's not working. Is there anything wrong here? How will I rename the folder?

For a PHP file:

Example 1

RewriteRule ^/?api/([^/]+)?$ "request.php?rquest=$1" [L,QSA]

Example 2

RewriteRule ^/?api/([^/]+)/([^/]+)?$ "request.php?rquest=$2&ns=$1" [L,QSA]

Where request.php is a file. It will be be referred example.com/api/{ns}/{rquest} in latter and example.com/api/{rquest} in former.

If you want to use a folder, as you requested, you may use like the following. Please test it out, as I have not used like this yet.

RewriteEngine on
RewriteBase /
RewriteRule ^/?apple/([^/]+)?$ "test/index.php?categories=$1" [L,QSA]

It is simply a rewrite rule. You define a pattern, and you then match it with what you want it to be rewritten. Simple as that.

Do you want to ignore the slash followed? That is like

example.com/help/legal/privacy/eula should be rewritten as example.com/help.php?path=/legal/privacy/eula , then you must ignore the / in the path followed by help in the URL pattern.

You can simply change the regex like the following:

RewriteRule ^/?help/(.+)?$ "help.php?topic=$1" [L,QSA]

So regex uses (.+) means, take and step everything and don't stop.

So this is the URL syntax:

RewriteRule {regex_of_url_pattern} "{rewrite_into_what}" [OPTIONS ...]


For more information and examples, please refer

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