5

The offending URLs are:

The .htaccess rule I have for these types of URLs looks like:

RewriteRule ^face/(.*)$ face.php?term=$1

What can I do to make both of these URLs to go to the same page?

Jake
  • 4,014
  • 8
  • 36
  • 54
  • 2
    `RewriteRule ^face/(.*)/?$ face.php?term=$1` – Grexis Apr 27 '12 at 08:01
  • You could just `rtrim($_GET['term'], '/')` inside your PHP script. Although @Grexis regex way is arguably better. – DaveRandom Apr 27 '12 at 08:05
  • @DaveRandom - Your solution is actually the one that worked. For some reason the .htaccess fix didn't work. Want to add as an answer so I can accept? – Jake Apr 27 '12 at 08:46
  • @Jake it would be best if you can find a .htaccess way - try @Grexis' rule but surround the `/` with parenthesis - `^face/(.*)(/+)?$` - or maybe `^face/(.*)/*$` – DaveRandom Apr 27 '12 at 09:11
  • Oh wait I have just realised what the problem is, it's a greedy `*`, try `^face/(.*?)/*$` – DaveRandom Apr 27 '12 at 09:30

2 Answers2

8

You can use this:

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.+)/$ /$1 [L,R=301]

The first line says: "if it's not a directory" (because then a trailing slash would have meaning). The second line says: redirect everything from start to the trailingslash and end to everything that was in there, without the trailing slash.

Put your own RewriteRule in there (below that one, not above) so your normal redirect still works after the trailing slash was removed.

(this one will obviously work for /body/ too, and not only for /face/.

Nanne
  • 64,065
  • 16
  • 119
  • 163
0

I use this rule which is slightly modified to maintain any subfolder structure

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
u01jmg3
  • 712
  • 1
  • 11
  • 31