返回基于目录基名的PHP页面而不重定向

时间:2011-03-13 03:21:32

标签: php redirect header pathinfo

我希望服务器根据目录名返回特定的PHP页面而不重定向。例如:

http://www.example.com/home

应返回与:

相同的结果
http://www.example.com/index.php?page=home

但不应重定向地址

4 个答案:

答案 0 :(得分:2)

您需要创建一个名为“.htaccess”的特殊文件,并将其放在Web应用程序根目录中。文件内容可能是这样的:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^/home$ /index.php?page=home [L]

然后配置Apache(httpd.conf,httpd-vhosts.conf)以允许您的Web应用程序使用该.htaccess配置文件

<VirtualHost 127.0.0.1:80>
    DocumentRoot "/path/to/your/webapp"
    ServerName localhost
    <Directory "/path/to/your/webapp">
        AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

这是一个很好的阅读。

http://httpd.apache.org/docs/current/misc/rewriteguide.html

答案 1 :(得分:1)

PHP无法在没有重定向的情况下执行此操作,但有许多解决方案:

将mod_rewrite与apache(.htaccess)一起使用

RewriteEngine on
RewriteRule ^home$ /index.php?page=home

另一个是将index.php添加到/ home,只有函数是包含文件根index.php。

答案 2 :(得分:0)

查看像(我最喜欢的)CodeIngniter或Zend这样的框架,但你不仅限于此,或者尝试通过自己的路由器重新发明轮子

答案 3 :(得分:0)

如果您使用的是Apache,可以查看htaccess脚本。通过谷歌搜索提供了大量选项

相关问题