【问题标题】:Return PHP page based on directory base-name without redirect根据目录基名返回 PHP 页面而不重定向
【发布时间】:2011-07-14 07:37:01
【问题描述】:

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

http://www.example.com/home

应该返回相同的结果:

http://www.example.com/index.php?page=home

但它不应该重定向地址

【问题讨论】:

    标签: php redirect header pathinfo


    【解决方案1】:

    您需要创建一个名为“.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

    【讨论】:

      【解决方案2】:

      如果没有重定向,PHP 无法做到这一点,但有很多解决方案:

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

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

      另一种方法是将 index.php 添加到 /home,唯一的功能是包含文档根 index.php。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          如果您使用的是 Apache,您可以查看 htaccess 脚本。谷歌搜索有很多可用的选项

          【讨论】:

            猜你喜欢
            • 2016-12-24
            • 2013-10-14
            • 2019-11-20
            • 2017-09-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-10
            • 2016-01-12
            相关资源
            最近更新 更多