.htaccessmod-rewrite

.htaccess in subfolder giving wrong parameter


I'm working on a subfolder in my website. The .htaccess in that folder has the following 2 lines:

RewriteEngine on
RewriteRule ^(.*)$                      page.php?id=$1 [L]

My php file in the subfolder has the following code:

print_r($_GET);

When I go to the following page:

https://www.example.com/subfolder/ddd

I get the following on the screen:

Array ( [id] => page.php )

What I want is:

Array ( [id] => ddd )

What am I doing wrong?


Solution

  • Keep in mind that the L flag only terminates the rewriting for the current loop of the rewriting process. Not fully. That is documented behavior.

    Rewriting will start over again if the request got rewritten by any rule. Here it does get rewritten from ddd to page.php?id=ddd, so in the next loop of the rewriting process that will get rewritten to page.php?id=page.php. That does not result in an altered request (the request to page.php is not altered). So the rewriting stops and you get the result you get.

    You have two immediate options to fix this:

    1.) Use the END flag instead of the L flag:

    RewriteEngine on
    RewriteRule ^(.*)$ page.php?id=$1 [END]
    

    This assumes that you have no other rules that need to get applied later. For example in other configuration files inside the file system tree.

    2.) Use an additional RewriteCond:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !page\.php$
    RewriteRule ^(.*)$ page.php?id=$1 [L]