【问题标题】:PHP redirecting all paths to a certain php file?PHP将所有路径重定向到某个php文件?
【发布时间】:2010-09-27 02:38:45
【问题描述】:

假设我的网站中有一个名为“test”的主文件夹,其中包含一个 index.php 文件 www.example.com/test/index.php

我怎样才能使包含此路径“www.example.com/test”的任何 url 重定向到“www.example.com/test/index.php”并仍然保留第一个请求路径

例如:

www.example.com/test/User       redirects to www.example.com/test/index.php
www.example.com/test/User/2     redirects to www.example.com/test/index.php
www.example.com/test/Account    redirects to www.example.com/test/index.php
www.example.com/test/Account/5  redirects to www.example.com/test/index.php

【问题讨论】:

    标签: php redirect


    【解决方案1】:

    使用 Apache 的Rewrite module。例如,在虚拟主机配置中,或在目录的 .htaccess 文件中,您可以这样写:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
    

    这将使任何接近该目录的 URL 重定向到 index.php,但不会更改脚本解析的 URL。


    这里有一些关于在其托管环境中使用重写的 Godaddy.com 文档:

    【讨论】:

    • 谢谢,我正在使用godaddy,知道在哪里可以找到“httpd.config”吗?
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多