【问题标题】:Rerouting all php requests through index.php通过 index.php 重新路由所有 php 请求
【发布时间】:2012-03-30 10:46:50
【问题描述】:

如何通过 index.php 重新路由对 php 页面的所有请求?

我的.htaccess如下:

Options +FollowSymLinks
IndexIgnore */*
#Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*\.(js|ico|gif|jpg|png|css|pdf)$ index.php%{REQUEST_URI} [L]

基本上,我正在尝试模拟 .NET 母版页。 index.php 有网站页眉/页脚。

它将 404 重定向到 index.php。如何让它将所有请求重定向到 php 页面(index.php 本身除外)?

此方法是否存在性能问题(不想使用框架)?

【问题讨论】:

标签: php .htaccess


【解决方案1】:

要将所有不存在的请求重定向到 /index.php ,您还可以使用 ErrorDocumentFallbackResource 指令。

尝试以下示例之一:

 ErrorDocument 404 /index.php


 FallbackResource /index.php

这些指令是 apache 核心模块的一部分,你不需要启用 mod-rewrite 来使用它们。

如果您正在寻找重定向所有请求(包括真实文件/目录)的方法,请尝试以下重定向

RedirectMatch ^/(?!index\.php).+)$ /index.php

【讨论】:

    【解决方案2】:

    用途:

    重写引擎开启 RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^(index\.php|public|css|js|robots\.txt) RewriteRule ^(.*)$ index.php/params=$1 [L,QSA] 错误文档 404 /index.php

    网址示例: www.site.com/friend/josh/03-13-2012

    所以 $params var 值:

    朋友/乔希/03-13-2012

    只需要explode() "/" 这样你就可以得到带有参数的数组:

    array(
    0 => friend
    1 => josh
    2 => 03-13-2012 
    )
    

    【讨论】:

    • 在 apache 2.4 上,您的答案对我有用,接下来的更改是:在 RewriteCond $1 ..... 我写而不是 $1% 这个: {REQUEST_FILENAME} 。我还添加了 END 标志:[END,L,QSA]
    【解决方案3】:

    这是我使用的(并且已经使用了很长时间):

    <IfModule mod_rewrite.c>
        # Redirect /index.php to / (optional, but recommended I guess)
        RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
        RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
    
        # Run everything else but real files through index.php
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
    </IfModule>
    

    正如 cmets 建议的那样,它会将不是实际文件的每个请求路由到 index.php

    【讨论】:

    • 主要部分看起来不错(并且与我所做的相匹配),但我认为将/index.php 路由到/ 的第一部分没有意义。因为在您的index.php 文件(处理所有 路由,包括index.php)中,如果您不想显示/index.php 的内容,只需 404....
    • 包含它的原因是您的主页不能在多个 URL 上使用。 mysite.com/ 和 mysite.com/index.php 不应同时存在,因为您可能会因重复内容而受到处罚。
    • 您可以为/index.php 输入302 以重定向回/
    • 嗯,我相信 301 在这种情况下是合适的。 mysite.com 是首选地址,而不是 mysite.com/index.php。对于 302,我们会说 mysite.com/index.php 是首选,而 mysite.com 只是临时的。
    • @powerboy,我是说该网站无论如何都不会在index.php 可用(如果你愿意的话)......将会发生的情况是脚本将被发送到index.php(因为它是一个现有文件),您可以在 PHP 代码中获取最初请求的 URL。这使得它在您的代码中很容易被唯一地识别。因此,您可以 302、404,任何您喜欢的处理方式,而无需在 .htaccess 文件中添加额外的 2 行。
    【解决方案4】:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !index\.php$
    RewriteRule .*\.php$ index.php?q=%{REQUEST_URI} [L]
    

    将捕获所有以 .php 结尾但不指向以 index.php 结尾的文件的请求,并将它们重定向到 get 参数中的 index.php q

    【讨论】:

    • 如果您不打算拥有静态文件夹/域,您可能应该说重写非文件。如果您不想发送文件;删除读取权限。这样现有的 css/js/whatever 文件将被发送。虚构文件将由 PHP 脚本处理。
    • +1 因为我知道你是对的。这是 op 要求的。
    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 2013-03-24
    • 2016-03-04
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    相关资源
    最近更新 更多