Solved

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

Posted on 2012-03-27
16
630 Views
Last Modified: 2012-03-30
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
0
Comment
Question by:Jules Webb
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 167 total points
ID: 37778130
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

0
 
LVL 2

Author Comment

by:Jules Webb
ID: 37778799
@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.
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 37780049
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]
0
 
LVL 57

Expert Comment

by:giltjr
ID: 37781609
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.
0
 
LVL 2

Author Comment

by:Jules Webb
ID: 37783836
@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!!
0
 
LVL 57

Expert Comment

by:giltjr
ID: 37785102
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.
0
 
LVL 2

Author Comment

by:Jules Webb
ID: 37785439
@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
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 37785453
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.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 2

Author Comment

by:Jules Webb
ID: 37785480
@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?
0
 
LVL 57

Expert Comment

by:giltjr
ID: 37785535
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.
0
 
LVL 2

Author Comment

by:Jules Webb
ID: 37785585
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".
0
 
LVL 23

Assisted Solution

by:Tiggerito
Tiggerito earned 166 total points
ID: 37785719
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.
0
 
LVL 57

Assisted Solution

by:giltjr
giltjr earned 167 total points
ID: 37786628
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.
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 37786990
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.
0
 
LVL 57

Expert Comment

by:giltjr
ID: 37787091
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.
0
 
LVL 2

Author Closing Comment

by:Jules Webb
ID: 37789423
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now