Problems stripping a trailing slash on non-directories

New to mod_rewrite? This is a good place to start.

Problems stripping a trailing slash on non-directories

Postby elia » Tue Sep 29, 2009 11:42 am

I was recommended here by someone at another forum, so I have high hopes my answer is just around the corner. :-)

Doing some work on my site this past week, I found a little issue where if someone goes to site.com/page/ it returns a 404 error. However, if they go to site.com/page the correct page is shown.

Here's what I have for rewriting URLs:
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . index.php [L]


It's working just as expected for sending everything through the index.php file if it's not an existing file or directory, but I'm wondering, is there a way that if the file isn't a directory, take off the trailing slash and send it through index.php as if it were site.com/page? I'm able to do what I'm looking for through PHP without a problem, but generally I like to keep all of my URL rules in the htaccess file.

Thanks much in advance!
elia
 
Posts: 2
Joined: Tue Sep 29, 2009 11:32 am

Postby richardk » Tue Sep 29, 2009 3:06 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# If it's not a directory
RewriteCond %{SCRIPT_FILENAME} !-d
# and it ends in a slash, redirect to remove it.
RewriteRule ^(.+)/$ /$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . index.php [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby elia » Tue Sep 29, 2009 5:45 pm

Works perfectly. Now I'm feeling a bit embarrassed for not realizing it right away. Thank you! :)
elia
 
Posts: 2
Joined: Tue Sep 29, 2009 11:32 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 31 guests

cron