从子目录重写页面

时间:2014-04-13 20:59:31

标签: php apache .htaccess mod-rewrite

我有以下设置:

index.php
config.php
   pages/
     index.php
     about.php
     contact.php

当我想访问" about.php"时,我需要访问http://host.com/index.php?page=about

现在我想重写网址,使其如下所示:http://host.com/about.php

.htaccess应该怎么样?我是mod_rewrite中的一个完整的菜鸟。

1 个答案:

答案 0 :(得分:0)

这应该适合你:

RewriteEngine On
#External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?page=(.*)\ HTTP
RewriteRule ^ /%2.php\? [R=302,L]

#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^(.*).php$ /index.php?page=$1 [L]

如果您访问http://example.com/index.php?page=about,则会将您重定向到http://example.com/about.php