ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Trailing Slash

October 29, 2012 11:09am

Subscribe [2]
  • #1 / Oct 29, 2012 11:09am

    MikeCJ

    47 posts

    My goal is to have each directory URL end with a trailing slash and each file URL end without one.

    Each internal link is written as an absolute URL followed by a trailing slash:
      1. The site index page: http://mysite.org/
      2. For each template group: http://mysite.org/template_group/
      3. For each template: http://mysite.org/template_group/template/

    When these links are referenced IE and Safari display all URLs with trailing slash. MOZ and Chrome display all URLs with trailing slash except for the site index page which is displayed without the trailing slash. Is this controllable or just a browser function?

    The only URLs I can control with a trailing slash are these internal links (with the exception of MOZ and Chrome on the site index page). How do I make sure other URLs will have a trailing slash when displayed in a browser?

    I am also not sure what URLs are actually files. I believe I remember that any URL generated through EE is NOT a file. Is this true? If so, how do I make sure every URL generated through EE ends with a trailing slash?

    I have entered the following code in the .htaccess file:

    #Code to add trailing slash
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !example.php
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://mysite.org/$1/ [L,R=301]
    But it doesn’t seem to do anything. When I type

    http://mysite.org/template_group
    in a browser I want the browser to add the trailing slash but it doesn’t.

    Where and how do I control the trailing slash.

    Thanks,
    MikeCJ

  • #2 / Oct 29, 2012 3:35pm

    launch

    7 posts

    Here is what I usually use:

    <IfModule mod_rewrite.c>
    
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    
    # REMOVE WWW
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    
    # ADD TRAILING SLASH
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ <a href="http://example.com/$1/">http://example.com/$1/</a> [L,R=301]
    
    # REMOVE index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
    </IfModule>

    The thing to remember is that the order of rewrites matters.  You must add the slash before removing the index.

  • #3 / Oct 29, 2012 4:15pm

    MikeCJ

    47 posts

    Changing up the order made all the difference. Here is what I now have:

    <IfModule mod_rewrite.c>
    
    # Enable Rewrite Engine
    # ------------------------------
    RewriteEngine On
    RewriteBase /
    
    #Redirect www to non-www
    Options +FollowSymlinks
    RewriteCond %{HTTP_HOST} ^www.mysite.org
    RewriteRule (.*) <a href="http://mysite.org/$1">http://mysite.org/$1</a> [R=301,L]
    
    #Code to add trailing slash
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ <a href="http://mysite.org/$1/">http://mysite.org/$1/</a> [L,R=301]
    
    # Redirect index.php Requests
    # ------------------------------
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    
    # Standard ExpressionEngine Rewrite
    # ------------------------------
    RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    </IfModule>

    If I type in IE, MOZ, Chrome, and Safari any URL generated by EE without the trailing slash the browser is adding the trailing slash with 2 exceptions: for mysite.org neither MOZ nor Chrome will add the trailing slash (but only for the index page). Any thoughts on this?

    Thanks,
    MikeCJ

     

  • #4 / Oct 29, 2012 4:29pm

    launch

    7 posts

    I think that you need to only include the

    RewriteEngine On
    RewriteBase /

    only once.  In the least, you do not need to turn the rewrite engine on twice.

    As far as the example.com/ I am not really sure.  Is there a specific reason that you need this, or is it just cosmetic?

  • #5 / Oct 29, 2012 4:41pm

    MikeCJ

    47 posts

    I cleaned up the code to read:

    <IfModule mod_rewrite.c>
    
    # Enable Rewrite Engine
    # ------------------------------
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    
    #Redirect www to non-www
    RewriteCond %{HTTP_HOST} ^www.mysite.org
    RewriteRule (.*) <a href="http://mysite.org/$1">http://mysite.org/$1</a> [R=301,L]
    
    #Code to add trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ <a href="http://mysite.org/$1/">http://mysite.org/$1/</a> [L,R=301]
    
    # Redirect index.php Requests
    # ------------------------------
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    
    # Standard ExpressionEngine Rewrite
    # ------------------------------
    RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    </IfModule>

    I am still getting the same results, so all good there.

    The reason for the trailing slash is to avoid any duplicate content issues. That’s the only reason.

    Thanks,
    MikeCJ

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases