简体   繁体   English

htaccess重命名文件夹路径

[英]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 ? 我知道我必须使用htaccess文件来执行此操作,但是我不明白如何解决该问题,或者给它提供了一种方法,可以在不更改文件夹结构的情况下更改php中的url路径? i have many files in this folder: 我在此文件夹中有很多文件:

localhost/sites/ 本地主机/网站/

example: 例:

localhost/sites/test.php 本地主机/网站/ test.php的

and i want to rewrite the url to: 我想将网址重写为:

localhost/test.php 本地主机/ 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: 假设您的网络服务器是Apache服务器:

Create a .htaccess file in your document root with the following content. 使用以下内容在文档根目录中创建一个.htaccess文件。 If it does not work please check that mod_rewrite is enabled. 如果不起作用,请检查是否启用了mod_rewrite。

The first RewriteRule redirects to the new URL and the RewriteCond makes sure this rule is only used for the external redirect. 第一个RewriteRule重定向到新URL,并且RewriteCond确保此规则仅用于外部重定向。

The second one makes sure the new URL actually works 第二个确保新URL确实有效

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]

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

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