Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing slash causes 404, can I fix using htaccess?

Tags:

php

.htaccess

The offending URLs are:

  • (Doesn't work) http://alltheragefaces.com/face/surprised-wut/
  • (Works) http://alltheragefaces.com/face/surprised-wut

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?

like image 761
Jake Avatar asked Apr 27 '12 07:04

Jake


People also ask

How do you fix trailing slash issues?

A 301 redirect is the best way to resolve duplicate content issues caused by trailing slashes. If you're just fixing one page, you'd redirect the duplicate copy to the version that matches your chosen URL structure. Most trailing slash issues however, affect many pages across a website.

Do trailing slashes matter URL?

Historically, a trailing slash marked a directory and a URL without a trailing slash at the end used to mean that the URL was a file. Today, however, trailing slashes are purely conventional, and Google does not care whether you use them; as long as you're consistent.

Should API have trailing slash?

Always use trailing slashes for any resource that may contain children. Just consider "GET" on a public_html directory with files.


1 Answers

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

like image 85
Nanne Avatar answered Sep 20 '22 10:09

Nanne