通过index.php重新发送所有php请求

fir*_*ird 15 php .htaccess

如何通过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]
Run Code Online (Sandbox Code Playgroud)

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

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

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

pow*_*uoy 20

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

<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>
Run Code Online (Sandbox Code Playgroud)

正如评论所示,它会将每个不是实际文件的请求路由到index.php

  • 包含该原因的原因是您的主页不能在多个URL上使用.mysite.com/和mysite.com/index.php不应该同时存在,因为您可能因重复内容而受到处罚. (3认同)
  • 嗯,我相信301在这种情况下是合适的.mysite.com是首选地址,而不是mysite.com/index.php.有了302,我们会说mysite.com/index.php是首选,而mysite.com只是暂时的. (3认同)

use*_*440 8

使用:


    
        RewriteEngine On
        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]
    

    
        ErrorDocument 404 /index.php
    

网址样本:www.site.com/friend/josh/03-13-2012

所以$ params var值:

朋友/乔希/ 2012年3月13日

只需要explode()"/"所以你得到params数组:

array(
0 => friend
1 => josh
2 => 03-13-2012 
)
Run Code Online (Sandbox Code Playgroud)