2
votes

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/foo/bar/123

but as long as I open it, the Apache redirects me to an error page. So I'm using a .htaccess. The code is:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)  /index.php [L]

and it works, as long as I use http://example.com/foo, but once I add some other parameters, it redirects me to an error.

I'm guessing that the rewrite code is wrong. Is it wrong?
If yes, could you suggest me the good one?
If no, where the problem could be located?

4

4 Answers

2
votes

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

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
votes

Since Apache 2.2.16:

FallbackResource /index.php