denlau
denlau

Reputation: 1016

Leading slash in URL with htaccess

I am currently trying to build a routing-script in PHP, where I am using htaccess to send URL-data to a PHP page.

My problem is, that I'm allowing all characters, which now means, that all URLs can have an unknown number of leading slashes.

My Htaccess is:

<IfModule mod_rewrite.c>

RewriteEngine On

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

RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>

This means, that this URL:

http://www.domain.com/sometext

Will give me the same $_GET['url'] as:

http://www.domain.com////////sometext

The requirements to my htaccess is, that I can actually use slashes in the string, BUT not in front :)

Does anyone know, what I am doing wrong? :)

UPDATE

In my root folder, I have this htaccess:

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    RewriteRule ^$    public/    [L]
    RewriteRule (.*) public/$1    [L]
 </IfModule>

And the in my /public folder, I have this htaccess:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+([^\?\ ]*)
RewriteRule ^ /%1 [R=301,QSA]

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

RewriteRule ^(.*)$ index.php?url=$1 [L]

</IfModule>

Upvotes: 1

Views: 560

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

For a URL like this:

http://www.domain.com////////sometext

The URI gets canonicalized before it's put through the regex in a RewriteRule. This means the URI gets turned into:

sometext

That means that the url parameter (i.e. $_GET['url']) should never have a ton of slashes in the front of the string. If this is a matter of seeing a ton of slashes in the browser's URL location bar, then you can do something like this:

RewriteCond %{THE_REQUEST} \ /{2,}([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301,QSA]

to get rid of the slashes in front.

Upvotes: 2

Related Questions