Add trailing slash if not present.

Hey guys,

I'm busy with a project, but because I'm such a bitch about details I
want to solve this.

I want to add a trailing slash to the url if it is not present.

for example:
http://mydomain.com/products/books

should be rewritten to:
http://mydomain.com/products/books/

Currently I have a few rules in my .htaccess file:

---
Options +FollowSymLinks

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|js|css)$ [NC]
RewriteRule ^(.*)$ index.php [L]
---

But how would I go about checking if it ends with a slash?

Greetz,

Wizz
Wizz [ Mi, 09 Januar 2008 14:27 ] [ ID #1903625 ]

Re: Add trailing slash if not present.

To elaborate on the current RewriteRules: I handle all the request
through a php script inside index.php. It does work now, I just think
it looks better with the trailing slash...

Greetz,

Wizz

On Jan 9, 2:27 pm, Wizz <woutaw... [at] gmail.com> wrote:
> Hey guys,
>
> I'm busy with a project, but because I'm such a bitch about details I
> want to solve this.
>
> I want to add a trailing slash to the url if it is not present.
>
> for example:http://mydomain.com/products/books
>
> should be rewritten to:http://mydomain.com/products/books/
>
> Currently I have a few rules in my .htaccess file:
>
> ---
> Options +FollowSymLinks
>
> RewriteEngine on
> RewriteBase /
>
> RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|js|css)$ [NC]
> RewriteRule ^(.*)$ index.php [L]
> ---
>
> But how would I go about checking if it ends with a slash?
>
> Greetz,
>
> Wizz
Wizz [ Mi, 09 Januar 2008 19:08 ] [ ID #1903629 ]

Re: Add trailing slash if not present.

"Wizz" <woutawizz [at] gmail.com> schreef in bericht
news:34ac85d4-b91d-4ffa-a00c-1d90e0497fb7 [at] 21g2000hsj.googleg roups.com...
>> On Jan 9, 2:27 pm, Wizz <woutaw... [at] gmail.com> wrote:
>> Hey guys,
>>
>> I'm busy with a project, but because I'm such a bitch about details
Yet another detail ... first quote than response is the usenet habbit

>> Options +FollowSymLinks
>>
>> RewriteEngine on
>> RewriteBase /
>>
>> RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|js|css)$ [NC]
>> RewriteRule ^(.*)$ index.php [L]
>> ---
>>
>> But how would I go about checking if it ends with a slash?
In fact how to test for being both extentionless AND trailing-slashless:
RewriteCond %{REQUEST_URI} !\.[^/]+$|/$ [NC]
RewriteRule (.*) $1/ [R,QSA]

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|js|css)$ [NC]
RewriteRule . index.php [L]

> To elaborate on the current RewriteRules: I handle all the request
> through a php script inside index.php. It does work now, I just think
> it looks better with the trailing slash...
If details do bother you rather have your script(s) and template(s) generate
the slash than make a browser ask twice.


HansH
HansH [ Mi, 09 Januar 2008 19:59 ] [ ID #1903630 ]

Re: Add trailing slash if not present.

"Wizz" <woutawizz [at] gmail.com> wrote in message
news:34ac85d4-b91d-4ffa-a00c-1d90e0497fb7 [at] 21g2000hsj.googleg roups.com...
> On Jan 9, 2:27 pm, Wizz <woutaw... [at] gmail.com> wrote:
>> Hey guys,
>>
>> I'm busy with a project, but because I'm such a bitch about details I
>> want to solve this.
>>
>> I want to add a trailing slash to the url if it is not present.
>>
>> for example:http://mydomain.com/products/books
>>
>> should be rewritten to:http://mydomain.com/products/books/
>>
<SNIP>
> To elaborate on the current RewriteRules: I handle all the request
> through a php script inside index.php. It does work now, I just think
> it looks better with the trailing slash...
>

You could do this in your php script with something like:
if (<no slash on the end>) {
Header("Location: http://mydomain.com/products/books/");
die;
}
phantom [ Do, 10 Januar 2008 10:15 ] [ ID #1904609 ]
Webserver » alt.apache.configuration » Add trailing slash if not present.

Vorheriges Thema: Strange thing
Nächstes Thema: WANdisco Announces Clustering Solution for Subversion and CVS