phpclean-urls

php version 7.1 clean urls


i have an index.php exactly like this

echo "<h2>index page</h2>";
$url=explode("/",$_SERVER['QUERY_STRING']);
echo "<pre>";
print_r($url);
print_r($_SERVER['QUERY_STRING']);
echo "</pre>";

and another one, in the same folder, profile.php with the same code

echo "<h2>profile page</h2>";
$url=explode("/",$_SERVER['QUERY_STRING']);
echo "<pre>";
print_r($url);
print_r($_SERVER['QUERY_STRING']);
echo "</pre>";

i'm trying to build an very simple route system with clean url's. When the url is like this http://localhost/vas/aaa/bbb i get from index.php the following result, which is OK:

index page Array ( [0] => aaa 1 => bbb ) aaa/bbb

printscreen image of index

but, when i type this: http://localhost/vas/profile/john/21, i include the profile in url as a first part of the url i get this:

profile page Array ( [0] => )

printscreen image of profile

That means that wihtout me doing any kind of routing, RUNS the profile.php and the result is an empty url-array without -the MOST important- the expected parameters e.g. john/21. Why is that, is routing, as functionality, embedded in php 7.* ? this is my .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?$1 [QSA,L]

Solution

  • Some explanations of your current .htaccess:

    The first 2 lines set up the condition for the url rewrite, in your case it will be rewritten if the specified folder or file in the url is not found:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    

    Then the rewrite rule specifies that the url that was passed should be transmitted to index.php as a query string:

    RewriteRule ^(.*)$ index.php?$1 [QSA,L]
    

    So if you don't have other .htaccess rules, when you call this url http://localhost/vas/profile/john/21, you're not supposed to end up in profile.php because you don't specify the file name in the url. You should end up in index.php with your url as parameter.

    However, if the folders vas/profile/john/21/ actually exist and there's a index.php in it, this is the file that will be called, but because the rewrite won't apply, you will not have your query string.

    There's probably something else happening here, other .htacces rules that redirect /profile/ to profile.php or what, that explains you arrive correctly to this file. Extra info could be useful..