SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Location
    Ireland
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add trailing slash

    Hi,

    Is it possible to write a rule that will add a slash to a URL that someone enters?

    My website is setup like this:

    www.domain.com/folder/

    If someone enters www.domain.com/folder the page will not load.
    www.domain.com/folder/ the page loads correctly.

    There is an index file in each folder.

    I have tried this but it does not work.

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

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    JB,

    Actually, your server should be setup to determine whether the request is a directory and then select the DirectoryIndex from that directory. In other words, complain to your host (first).

    Then, to show your host that you're smarter than they are, REMOVE the useless RewriteBase directive, test for the request being a directory THEN redirect to add the trailing /, i.e.,
    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .? http://www.example.com%{REQUEST_URI}/ [R=301,L]
    When you create a specification, you can see the clearest path to actually writing the code. You wanted to add the trailing / to a directory request so that should have been your test and everything else just falls into place.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •