How do I force a trailing slash via the htaccess file?

Hello

I'm trying to force a trailing slash via the htaccess file without success. Below is the code I thought would work.

# Force trailing slash
<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_URI} /+[^\.]+$
 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>

Open in new window


Thanks for the help!
Jules
LVL 2
Jules WebbFreelance Web DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sammySeltzerCommented:
I am not very good at php but what about this?

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.mydomain.com/$1/ [L,R=301]

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jules WebbFreelance Web DeveloperAuthor Commented:
@sammySeltzer

I am not very good at php but what about this?

This is actually apache not php.  The code you presented looks like instructions to remove the index.php file and force www.
Tony McCreathTechnical SEO ConsultantCommented:
I think sammys solution may work. To translate:

Don't process the index.php file
Don't process URLs that already end in a slash
If we made it, Redirect the URL to one with the slash at the end.

If you don't want the www forced this change may work:

RewriteRule ^(.*)$ /$1/ [L,R=301]
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

giltjrCommented:
Why do you want to force a trailing slash?

Are you sure you want to force a trailing slash?  Meaning

     http://www.domain.com/somefile.html

will be forced to:

     http://www.domain.com/somefile.html/

Which I am fairly sure will cause problems.
Jules WebbFreelance Web DeveloperAuthor Commented:
@giltjr

Note: I'm sure my goal is obvious, but just to be clear, I'm trying to present one canonical url for search engines using best practices

None of the pages on the site end with an extension (.php or .html)

I used to always force no slash until this linked in conversation:
http://www.linkedin.com/groups/Canonical-URL-question-Trailing-slash-53266.S.88719158?trk=group_search_item_list-0-b-ttl&goback=%2Egna_53266

Where Bill Marshal stated
If you omit the trailing slash then the server has to make one extra response to get to the correct address because it can't tell if the address is for a directory or a file.

I did some research at http://developer.yahoo.com/performance/rules.html where I found:
Avoid Redirects

One of the most wasteful redirects happens frequently and web developers are generally not aware of it. It occurs when a trailing slash (/) is missing from a URL that should otherwise have one. For example, going to http://astrology.yahoo.com/astrology results in a 301 response containing a redirect to http://astrology.yahoo.com/astrology/ (notice the added trailing slash). This is fixed in Apache by using Alias or mod_rewrite, or the DirectorySlash directive if you're using Apache handlers.


@Tiggerito
Thank you for the translation!

@sammySeltzer
Your solution worked, but I really want to hear from @giltjr before I close this question. At this point I am not feeling confident about slash or no slash.

Thank you all for your help and input!!
giltjrCommented:
What they are talking about on the 301 error is if the URI is a folder instead of a file.

In their example of:

      http://astrology.yahoo.com/astrology

and

     http://astrology.yahoo.com/astrology/

If somebody enter

     http://astrology.yahoo.com/astrology/index.html

That would not generate a 301 because that actually exists.  However adding a trailing slash to produce

     http://astrology.yahoo.com/astrology/index.html/

Would cause problems.  Why?  because if you have a relative href it is based on the current "directory/folder" level.   For:

          http://astrology.yahoo.com/astrology/index.html

That would be http://astrology.yahoo.com/astrology/.  However for  

          http://astrology.yahoo.com/astrology/index.html/

It would be http://astrology.yahoo.com/astrology/index.html/.  Because it is assumed that if the URI ends in a slash you are referring to a  directory/folder.

--> None of the pages on the site end with an extension (.php or .html)

It does not matter what the file name ends it, php, html, htm, text, pdf, whatever.  You

Now after that explanation, what sammySeltzer provided you, should work  Because the first condition (!-f) checks to see if that URI is a not file.  If it is a file, the condition fails and nothing else is checke, the rewrite is NOT done.
Jules WebbFreelance Web DeveloperAuthor Commented:
@giltjr

Thank you for your detailed explanation.

When I said "None of the pages on the site end with an extension (.php or .html)" I probably should have been more clear. The site pages don't end in any extension:  mysite.com/about   mysite.com/blog   mysite.com/blog/myblogtitle

so what is the correct thing to do if it's a page, but has no extension? Should mysite.com/blog/myblogtitle have a slash at the end or not?

NOTE: the site is built using the CMS Expression Engine and the pages are actually pulled in from a database.

I really appreciate getting clarity on this issue, thanks.

Jules
Tony McCreathTechnical SEO ConsultantCommented:
I personally go for the option without the trailing slash. It's easier to spell out and for people to manually enter.

It would also be easier to strip out a trailing slash as it does not introduce the possible problems stated above:

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

If its a non zero length path plus a slash then capture the path without the slash and redirect to it. Not tested.
Jules WebbFreelance Web DeveloperAuthor Commented:
@Tiggerito

I personally go for the option without the trailing slash. It's easier to spell out and for people to manually enter.

a user wouldn't have to remember the trailing slash, it would still go the the correct page and then the rule would force the slash

In the past I've forced removal of the trailing slash, but after that conversation on linkedin I thought I was wrong.

So what is the best practice?
giltjrCommented:
How many 301's do you see in your log?  Ton's, many, a few?

Better yet, if you are going to go through the overhead of doing a URL rewrite, to add a slash to the end, why not rewrite with the real default html file?  That is instead of changing:

     http://www.myhost.com

to

     http://www.myhost.com/

why not

     http://www.myhost.com/index.html

Or what ever the default is.
Jules WebbFreelance Web DeveloperAuthor Commented:
why would I want index.html?

users will type in mysite.com not mysite.com/index.html

I want "pretty" urls and I want to either force or not force a slash for the same reason you either force www.mysite.com mysite.com. I want one present one canonical url.

How many 301's do you see in your log?  Ton's, many, a few?
Seeing 301's in my log isn't the problem. I was just trying to set up the site using "best practices".
Tony McCreathTechnical SEO ConsultantCommented:
a user wouldn't have to remember the trailing slash, it would still go the the correct page and then the rule would force the slash

They would then suffer a small delay due to the redirect. If a user creates a link without the slash then this redirect will also cause a small leak in pagerank.

Obviously the opposite is also true but I'd suspect accidentally adding a slash is far less common than accidentally forgetting it.

In the past I've forced removal of the trailing slash, but after that conversation on linkedin I thought I was wrong.

My take on that post (which I only scanned) was the consensus is pick one and stick with it. There is no big difference. The problem the person had was he was not being consistent. He chose to use one but the canonical tag and redirect mechanism was enforcing the other.

So what is the best practice?

Whichever you chose:

Have all links using it.
Have all canonical tags referring to it.
Ensure redirects enforce it.

    http://www.myhost.com

to

     http://www.myhost.com/

These represent the exact same page. The domain slash is implicit. You can't actually do a redirect for this.
giltjrCommented:
I just read the Linked In discussion and agree with Tiggerito comments.

The main point I think "Jim" was making, which  Tiggerito stated.  Pick one and stay with it.

From what Jim was saying the main issue is not performance, but Google Rankings.  Google will see:

     http://www.myhost.com

different from

    http://www.myhost.com/

Even though they are the same exact page.  Review you logs, do you have a lot of 301's.  If you do, then add the rewrite to add the trailing slash.  Remember it only deals with URI's that do not specify a specific page.  That is, it only matters when displaying the default page for a directory/folder.  For any URI that contains a file name, you are going to go through the rewrite code just to do nothing.  That is why it is import that if you are going to do this you have:

     RewriteCond %{REQUEST_FILENAME} !-f

As your first condition.
Tony McCreathTechnical SEO ConsultantCommented:
I'd love to see any evidence that Google or anything on the internet sees:

   http://www.myhost.com

different from

    http://www.myhost.com/

As this doe not make sense based on the internet protocols.

Each URI is based of parts with character sequences that divide them. The protocol splits those parts up to determine each step of the route to the destination. A slash after the domain means the root of the domain, just as no slash indicates. They are exactly the same place, no matter what you do.

Base level IP layers deal with this for you so by the time it reaches things like the .htaccess handler the slash is never there. look at .htaccess conditions and they never include the slash!

This changes when you select the destination in the .htaccess file. We are back to relative URLs which have to be translated back to an absolute URL. Omitting the initial slash means redirect relative to the folder I am already in.
giltjrCommented:
Doesn't make sense to me either.  Although I really don't do a whole lot of actual web page setup, I never even heard of this issue until this question.
Jules WebbFreelance Web DeveloperAuthor Commented:
Thank you all for your help.  

sammySeltzer your solution did work

giltjr & Tiggerito thanks for taking the time to explain things to me. I think I'll go back to forcing no trailing slash. Since an added trialing slash should only show up occasionally redirects for this should be at a minimum, and I keep my URL's consistent.

Cheers!
Jules
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.