0
votes

I know there have been plenty of "remove trailing slash" via Apache URL rewriting questions on here, but I still haven't found a way (via RewriteCond/RewriteRule or RedirectMatch) to do what seems to be a really simple trailing-slash redirect for only the root url on the host:

http://www.example.com/ -> http://www.example.com

The only reason I want to do this is for SEO - I've been told that redirecting to a single canonical home URL is the way to go for google's crawler.

The problem is that ${REQUEST_URI} ALWAYS contains a slash (meaning the 2 URLs above have identical ${REQUEST_URI} values of /), and RewriteRule / RedirectMatch operate off of ${REQUEST_URI}. RewriteCond can use other server variables, but I don't see one that just matches the entire URL with the host AND the empty request.

Since I can't differentiate between / and empty, I can't remove the trailing slash without causing an infinite redirect loop.

# Causes infinite redirect loop
RewriteRule ^(.*)/$ $1 [R,L]

Any thoughts on how to do this? Is this something extremely simple that I'm just missing?

1
that means nothing for the HTTP. The request itself must contain at least a root path /. You talk about indexed textual fields of link-tags.Deadooshka

1 Answers

0
votes

You can do that using the following rule:

#If example.com is present, then remove /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

Make sure you clear your cache before testing this.