2017-04-23 154 views
0

我想重写我的网站的网址。我知道我必须做到这一点与htaccess文件,但我不明白我可以如何解决它,或给它一种方式如何我可以在不改变文件夹结构的PHP更改url路径?我有很多文件,此文件夹中:htaccess重命名文件夹路径

本地主机/网站/

例如:

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

,我想重写地址为:

本地主机/ test.php的

,但我不想要移动的文件。

+0

我更新的答案现在实际工作 – Thakkie

回答

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

它的工作原理,但如果在浏览器中写/sites/test.php它不会更改URL来进行测试。 php –

+1

请编辑您的答案以包含一些解释。仅有代码的答案对未来SO读者的教育很少。您的回答是在低质量的审核队列中。 – mickmackusa

0

编辑

我以前的答案是行不通的。现在确实如此。

假设你的Web服务器是Apache服务器:

创建.htaccess文件在你的文档根目录下面的内容。 如果不起作用,请检查是否启用了mod_rewrite。

第一个RewriteRule重定向到新的URL,RewriteCond确保此规则仅用于外部重定向。

第二个确保新的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]