1

My .htaccess file is like this:

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

And my index.php:

echo '<pre>';
print_r(explode('/', $_SERVER['REQUEST_URI']));
echo '</pre>';

When I go to example.com/test/one/two, it gives me back:

Array
(
    [0] => 
    [1] => test
    [2] => one
    [3] => two
)

Why is there a 0?

1 Answer 1

1

Because your $_SERVER['REQUEST_URI'] is /test/one/two.

To get rid of that you could remove first slash from $_SERVER['REQUEST_URI']:

substr($_SERVER['REQUEST_URI'], 1);
Sign up to request clarification or add additional context in comments.

3 Comments

But why in the array is there a 0. I know arrays starts with a 0. Why is 'test' not the beginning of the array?
Because there is an empty string in front of the first slash
explode function splits string on boundary formed by delimiter parameter. So when you have your delimiter as first character in string it puts an empty string into a resulting array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.