Search code examples
php.htaccess

Htaccess rewrite removes trailing slashes


Htaccess somehow automatically removes all trailing slashes at the end of an url and keeps only one.

For example http://localhost/api/param1/// becomes http://localhost/api/param1/

Can you please tell me why this happens and how to get rid of this? The (.*) should match everything right? But it does not. Like I said, if I go to http://localhost/api/param1/// the $_GET['url'] should be param1/// but it is param1/.

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

Solution

  • Apache automatically strips multiple slashes into a single slash in RewriteRule pattern.

    If you want to capture multiple slashes use a RewriteCond instead:

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