3

This is an .htaccess question which I found the answer to but, I need to integrate it with the .htaccess that comes with Craft.

I need to add trailing slashes to the end of all URLs.

I know there is a General Config Setting for adding trailing slashes to URLs to "dynamically-generated URLs" but, in the case there are any hard coded urls with out the trailing slashes or if someone links to the site without trailing slashes it would be nice to set it and forget it via the htaccess.

For example:

http://mydomain.dev/news/news-entry

would 301 redirect to

http://mydomain.dev/news/news-entry/

This is what comes with Craft by default:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

2 Answers 2

5

Craft has a config setting for this (for dynamic urls)

You can set it in your config file like so:

'addTrailingSlashesToUrls' => true

If someone links to the url via a non-trailing-slash pattern, it should redirect.

If you use an apache rewrite, you can exclude the craft admin by checking the uri pattern

RewriteCond %{REQUEST_URI} !(admin)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
4

I found by just adding the code from the following site it will add slashes via a 301 redirect:

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

Now my .htaccess file looks like the following:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 301 redirect with trailing slash
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>

And here is another version/option which seems to work as well:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

Here is a third option which seems to work as well:

# Always append a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R=301,L]

I'll update once I've researched and settled upon the best option.

2
  • Note: these need to go before the default Craft rewrite.
    – Natetronn
    Apr 29, 2015 at 0:54
  • I don't think it should be adding this onto the dashboard URLs. We'll have to come up with a solutions which takes that into consideration.
    – Natetronn
    May 22, 2015 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.