Rewrite - following forward slash

Discuss practical ways rearrange URLs using mod_rewrite.

Rewrite - following forward slash

Postby wwfc_barmy_army » Sun Aug 23, 2009 3:25 am

Hello.

I have this rewrite code:

Code: Select all
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)$ index.php?id=$1&width=$2&height=$3 [L,QSA]


It works if i go to say:
mydomain.com/1
but not if i go to:
mydomain.com/1/

To take care of the following slash can i just do this:

Code: Select all
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)$ index.php?id=$1&width=$2&height=$3 [L,QSA]
RewriteRule ^([0-9]+)/$ index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/$ index.php?id=$1&width=$2&height=$3 [L,QSA]


Would that work or cause errors?

Thanks.
wwfc_barmy_army
 
Posts: 7
Joined: Fri Aug 14, 2009 5:32 am

Postby richardk » Mon Aug 24, 2009 2:04 pm

You can use "/?" to make the trailing slash optional. And unless you have files or directories that are numerical you don't need the conditions.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [QSA,L]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/?$ /index.php?id=$1&width=$2&height=$3 [QSA,L]


Would that work or cause errors?

It should work, have you tried it?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby wwfc_barmy_army » Tue Aug 25, 2009 12:36 am

Thanks. Your way seems to have worked :) Thanks for your help richard :)
wwfc_barmy_army
 
Posts: 7
Joined: Fri Aug 14, 2009 5:32 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron