1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I prevent trailing slash in 301

Discussion in 'Apache' started by mark_s, Jul 8, 2009.

  1. #1
    Please can someone tell me how I can prevent this redirect adding a slash after .php when people decide to use a trailing slash at the end of the URL.

    RewriteEngine on
    Redirect 301 /test http://www.domain.com/test.php
    Code (markup):
    http://www.domain.com/test correctly redirects to the PHP web page.

    http://www.domain.com/test/ incorrectly adds a trailing slash at the end of the PHP web page causing the page not to load properly.
     
    mark_s, Jul 8, 2009 IP
  2. zain654321

    zain654321 Peon

    Messages:
    202
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Multiple chained redirects should be avoided if you want to pass the "link juice."
    There are two ways to do this.

    The first approach is to write each rule so that it accepts all permutations of input URL and redirects to the canonical URL all in one go. This is useful for a limited set of input URL permutations with a limited number of canonical URLs.

    The second approach, likely more suited to your situation, is to specify a redirect in each rule, but to hold off invoking it until all fix-ups on the output URL are done. Then a final rule checks to see if any fix-ups were done, and if so, invokes the actual external redirect. To do this, you can set an environment variable in each fix-up rule to indicate that the external redirect should be invoked. For example:

    # Redirect to add trailing slash if no trailing slash and no period in final URL-path-part of requested URL
    RewriteRule ^(([^/]+/)*[^/.]*[^/])$ http://www.example.com/$1/ [R=301,E=doRed:Yes]
    #
    # Redirect to canonical hostname if non-canonical hostname requested
    RewriteCond %{HTTP_HOST} !^www\.example\.com$
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,E=doRed:Yes]
    #
    # invoke an external redirect if any of the above fix-ups were applied
    RewriteCond %{ENV:doRed} ^Yes$
    RewriteRule ^(.*)$ - [R=301,L]

    An important guideline that you should follow to avoid unexpected results and problems: Put all external redirects first, in order from most-specific patterns and conditions (fewest URLs affected) to least-specific pattern (most URLs affected), followed by all internal rewrites, again in order from most- to least-specific. Where patterns and conditions are different and mutually-exclusive, then order won't matter.

    This is illustrated by the fact that in the code above, only URLs which don't end in a slash and don't contain a period in the final URL-path-part (indicating that a filetype is not present) are redirected to add a slash. (Side note: doing this based on the absence of a filetype is much more efficient than checking the disk for "file exists" -- possibly thousands of times more efficient.)

    This rule is then followed by the domain canonicalization rule, which will redirect *all* URLs if any non-canonical hostname is requested.

    If any internal rewrites are present, they must follow all external redirects. Otherwise, a redirect will expose the internally-rewritten filepath as a URL -- almost always an unwanted result.

    The code posted here is for use in .htaccess, as the majority of our readers are on shared name-based servers with no access to their server config files. For use at the server config level, add a leading slash to the regular-expressions patterns in the RewriteRules (only) -- e.g. "^(.*)$" becomes "^/(.*)$"

    A bug exists in all versions of Apache mod_rewrite which can cause errors when multiple sequential rewrites are done. The result is that part of the URL gets re-injected into the substitution path, and you see "duplication" of parts of the URL-path. It's generally only a problem with internal rewrites, though. If this does occur, be sure to post back here; There is a solution, but it's ugly, inefficient, and unnecessarily complicated unless needed.
     
    zain654321, Jul 8, 2009 IP
  3. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Right so what changes do I need to make to my redirect? I don't care about trailing slashes apart from this particular redirect.
     
    mark_s, Jul 9, 2009 IP
  4. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've just read your post again, you've given me the opposite of what I need. I need the trailing slash to be removed for the result of only this particular redirect otherwise the .php will end with a trailing slash causing problems.
     
    mark_s, Jul 10, 2009 IP