简体   繁体   中英

htaccess rename folder path

i want to rewrite the url from my website. I know i must do it with the htaccess file but i dont understand how i can solve it, or give it a way how i can change the url path in php without change the folder structure ? i have many files in this folder:

localhost/sites/

example:

localhost/sites/test.php

and i want to rewrite the url to:

localhost/test.php

but i dont want to move the files.

RewriteEngine On 
RewriteRule ^(.*) sites/test.php [L]

Edit

My previous answer was not working. Now it does.

Assuming your webserver is an Apache server:

Create a .htaccess file in your document root with the following content. If it does not work please check that mod_rewrite is enabled.

The first RewriteRule redirects to the new URL and the RewriteCond makes sure this rule is only used for the external redirect.

The second one makes sure the new URL actually works

RewriteEngine On

# Check that the request URI is sites/test.php
RewriteCond %{REQUEST_URI} ^sites/test.php
# Redirect the browser to the new URL
RewriteRule ^sites/test.php /test.php [R,QSA,L]

# Rewrite the new URL to use the file in the old location    
RewriteRule ^test.php home/test.php [QSA,L]

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