Rerouting all php requests through index.php

前端 未结 4 1756
误落风尘
误落风尘 2020-11-28 07:20

How can I reroute all requests for php pages through index.php?

My .htaccess is as follows:

Options +FollowSymLinks
IndexIgnore */*
#Turn on the Rewr         


        
相关标签:
4条回答
  • 2020-11-28 07:36

    Here's what I use (and have used for ages):

    <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>
    

    As the comments suggest it will route every request that isn't an actual file to index.php

    0 讨论(0)
  • 2020-11-28 07:36

    Use:

    
        
            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
        
    
    

    Url sample: www.site.com/friend/josh/03-13-2012

    So the $params var value:

    friend/josh/03-13-2012

    Only need explode() "/" so u get array with params:

    array(
    0 => friend
    1 => josh
    2 => 03-13-2012 
    )
    
    0 讨论(0)
  • 2020-11-28 07:36

    To redirect all non-existent requests to /index.php , You can also use ErrorDocument and FallbackResource directives .

    Try one of the following examples :

     ErrorDocument 404 /index.php
    
    
     FallbackResource /index.php
    

    These directives are part of apache core modules and you dont need to enable mod-rewrite to use them.

    If you are looking for a way to redirect all requests (including real files/dirs) then try the following Redirect

    RedirectMatch ^/(?!index\.php).+)$ /index.php
    
    0 讨论(0)
  • 2020-11-28 08:00
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !index\.php$
    RewriteRule .*\.php$ index.php?q=%{REQUEST_URI} [L]
    

    Will catch all requests ending in .php that don't point to a file ending in index.php and redirect them to index.php in the get parameter q

    0 讨论(0)
提交回复
热议问题