使用mod_rewrite重写url并删除子目录

时间:2014-04-30 10:06:49

标签: php apache .htaccess mod-rewrite

我目前正在建立一个网站,它还没有上网,我正在使用wamp服务器2.4。我需要删除子目录以便不泄露我的目录结构,我有一个带有以下文件夹的测试站点: includes sub

要访问的链接位于 sub 中,因此如果用户点击转到sub/link.php的链接,则网址会显示localhost/sub/link.php ..我希望它显示localhost/test/link/没有子显示。到目前为止,我已经在Apache中用Google搜索了mod_rewrite,了解了它但仍然无法删除子目录。我所取得的就是删除文件扩展名(.php)。

我在这里搜索了Stack Overflow,但答案并没有帮助,因为他们使用的是在线托管网站。 link.php 没有变量,它是一个简单的php文件,可以回显东西,但它没有使用任何GET / POST变量。

这是html代码,我故意在'http'中写't'但是在代码中它是两个..

链接

RewriteEngine On
RewriteRule ^sub/(.*)$ /$1 [L,R=301]

看看这个网站,它是在wordpress上托管的,你去的每个链接都显示没有任何子目录,它只是在网址后面有一个斜杠,是否可以在apache上实现呢?

http://www.afdar.com

1 个答案:

答案 0 :(得分:0)

.htaccess

中试试这个
RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/test/sub/
RewriteRule ^test/(.*)$ /test/sub/$1 [QSA,L]

它会重写网址:

http://localhost/test/sub/link.phphttp://localhost/test/link.php

http://localhost/test/sub/another.phphttp://localhost/test/another.php

相关问题