5

I have just upgraded from EE 1 to EE 2 and I am struggling with some of the changes. For example, the fact that EE no longer outputs a trailing slash on its urls is making a mess of a lot of my links where I had depended on constructions like EE generated url + additional segment. Finding and editing all places where I have done that would be a small nightmare considering the size and setup of my site. Is there any way to hack EE to get back the old behaviour?

Linda Antonsson
  • 283
  • 1
  • 7
  • can you explain a bit more "constructions like EE generated url + additional segment"? – GDmac Nov 03 '12 at 21:55
  • For example, I had an issue with a template where I was using the Tags module and I needed to setup a link like this:"{path='Characters/Season'}/{websafe_tag}/". Originally, I did not need the / before {websafe_tag} since the first part of the url was generated with a trailing slash. So I have depended on that in a lot of instances. – Linda Antonsson Nov 03 '12 at 23:09
  • 1
    Looks like {path=} and {url_title_path} rely on `$this->EE->functions->create_url($path)`, so that might be a place to hack an extra slash in. However i would recommend a search and replace in the templates above hacking core EE. – GDmac Nov 03 '12 at 23:28
  • I would go for a search and replace if I knew off the top of my head all the various combinations where I've used a similar construction, but we're talking templates built up over 6 years and about 400 of them, so it is daunting to say the least. I will take a look at that function, thank you. :) – Linda Antonsson Nov 04 '12 at 00:31

2 Answers2

6

For some the trailing slash addition mentioned might cause conflicts with some forms. Adding the following to check if the request is a GET might be safer.

# Add a trailing slash to paths without an extension
RewriteCond %{THE_REQUEST} ^GET
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
Parscale
  • 181
  • 2
  • I've finally had time to test this and I am a bit mystified as to how this actually works. With it added to my .htaccess, if I click a link on one of my pages, the new page still loads without a trailing slash. – Linda Antonsson Nov 21 '12 at 12:20
0

There is an .htaccess solution to this, which I've used in my older EE sites because of this issue exactly.

Add trailing slash

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

Source: http://devot-ee.com/articles/item/simple-htaccess-for-expressionengine-sites

However, since it's there by default in EE2, you might want to do the OPPOSITE and remove the trailing slash via .htaccess so you don't have to manually add/delete/whatever. If you decide on that, here's the code:

Remove Trailing Slash

RewriteCond %{HTTP_HOST} !^\.yoursite\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

Source: http://ee-spotlight.com/tips/a_standard_htaccess_file_with_expressionengine

lealea
  • 563
  • 2
  • 10
  • The problem isn't so much at the end of URLs, unfortunately, it is when I end up with a missing trailing slash inside of a URL. I have posted an example in an earlier comment which I hope illustrates what I mean. The .htaccess solution sounds great for normalising the end of URLs to either have or not have a trailing slash, but it doesn't seem like it could solve the missing slashes within URLs. – Linda Antonsson Nov 03 '12 at 23:14