SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Force or remove trailing slash?

    I'm using a rewrite for friendly urls, if the page is accessed without a trailing slash it throws a 404 error.

    Is it possible to either force the trailing slash so it is impossible to access it without a slash and get an error, or force remove the trailing slash and access url without it.

    here is what i have so far

    Code:
    Options -Indexes -MultiViews +FollowSymLinks  
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)$ index.php?pages=$1&action=$2 [L]
    thanks

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Hi Uplift,

    You might try this
    Code:
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [R=301,L]
    Last edited by ServerStorm; Aug 10, 2012 at 10:11.
    ictus==""

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it didnt seem to work

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

    IMHO, the server should throw an error with a trailing / on a filename (you're not using MultiViews) just the same way that the server should throw an error if you request a directory without the trailing / (servers have been written to compensate for this error, though).

    Quote Originally Posted by Uplift View Post
    Is it possible to either force the trailing slash so it is impossible to access it without a slash and get an error, or force remove the trailing slash and access url without it.
    Anything is possible and I'd say that it depends upon the server's configuration. What you have seems ...

    Code:
    Options -Indexes -MultiViews +FollowSymLinks  
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^
    # empty string? That seems useless to me
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)$ index.php?pages=$1&action=$2 [L]
    # That doesn't do what you're asking for ... and could cause problems with your EVERYTHING
    # OR NOTHING metacharacter in both atoms. Why aren't you using + (one or more)?
    Steve's code could use a little help although, technically, it's not wrong:

    Code:
    RewriteCond %{SCRIPT_FILENAME} !-d
    # There is a subtle difference between {SCRIPT_FILENAME} and {REQUEST_FILENAME}
    # with {REQUEST_FILENAME} being the preferred variable here
    RewriteRule ^(.+)/$ /$1 [R=301,L]
    # The leading / in the redirection tells Apache to look in the server's root directory
    # before looking in your DocumentRoot - a very dangerous thing to do especially
    # without validating the submitted {REQUEST_URI} string. However, for your
    # solution, I'd merely omit the / and look for a way to eliminate hack attacks
    # with the (.+) regex (like eliminating the dot character).
    Saying that it doesn't work doesn't help. Please provide the test URI and what was returned from the server as well as the exact .htaccess you were using. We can't help you troubleshoot without useful information.

    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
  •