How to allow three optional parameters in the URL by .htaccess?

后端 未结 4 1937
一整个雨季
一整个雨季 2020-12-21 14:48

I have http://example.com and a PHP routing class that checks if some URL exists.
I want to make a new route, which is:

http://example.com/

相关标签:
4条回答
  • 2020-12-21 15:23

    I guess this could help. "/index.php" will delegate to the wrong path.

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    
    0 讨论(0)
  • 2020-12-21 15:26

    Since Apache 2.2.16:

    FallbackResource /index.php
    
    0 讨论(0)
  • 2020-12-21 15:28

    You can handle the URI with php:

    .htaccess:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    

    php:

    if (isset($_SERVER['REQUEST_URI']))
    {
        $params = explode("/", ltrim($_SERVER['REQUEST_URI'], "/"));
        print_r($params);
    }
    

    example.com/just/these/params

    Output:

    Array
    (
        [0] => just
        [1] => these
        [2] => params
    )
    
    0 讨论(0)
  • 2020-12-21 15:39
    RewriteRule ^(.*)$  /$1.php [L]
    

    Try it like this. In you .htaccess every request that isn't a file on disk is redirected to index.php, I am guessing that index.php is redirecting further. I don't think you can get this route /foo/bar/123 or /foo/bar with just one line of code. Every page needs it own rewrite rule. Could you be more specific?

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