Add trailing slash if not present.

Add trailing slash if not present.

am 09.01.2008 14:27:24 von Wizz

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

Re: Add trailing slash if not present.

am 09.01.2008 19:08:13 von Wizz

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 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

Re: Add trailing slash if not present.

am 09.01.2008 19:59:22 von HansH

"Wizz" schreef in bericht
news:34ac85d4-b91d-4ffa-a00c-1d90e0497fb7@21g2000hsj.googleg roups.com...
>> On Jan 9, 2:27 pm, Wizz 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

Re: Add trailing slash if not present.

am 10.01.2008 10:15:52 von phantom

"Wizz" wrote in message
news:34ac85d4-b91d-4ffa-a00c-1d90e0497fb7@21g2000hsj.googleg roups.com...
> On Jan 9, 2:27 pm, Wizz 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/
>>

> 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 () {
Header("Location: http://mydomain.com/products/books/");
die;
}