Jake
Jake

Reputation: 4234

Trailing slash causes 404, can I fix using htaccess?

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?

Upvotes: 5

Views: 4377

Answers (2)

u01jmg3
u01jmg3

Reputation: 732

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]

Upvotes: 0

Nanne
Nanne

Reputation: 64439

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

Upvotes: 8

Related Questions