Forum Moderators: phranque

Message Too Old, No Replies

redirect with and without trailing slash

         

twisty80

2:50 pm on Dec 18, 2004 (gmt 0)



Hi there,

I was wondering if it is possible to redirect an url with the trailing slash and without it to the same destination which is a simple script using mod_alias.

Like:
[somehost.com...] => [otherhost.com...]
[somehost.com...] => [otherhost.com...]

Is there any (performance, security) disadvantages using mod_rewrite?

thanks in advance,

tw

sublime1

11:14 pm on Dec 18, 2004 (gmt 0)

10+ Year Member



I would just use RewriteRule and RewriteCond -- I was at one time worried about the performance of rewrites, but I have used Apache on seriously high volume sites (millions of queries per day -- I ran engineering for a company that provided search results to major search engines back before there were only several :-). RewriteRules seem like black magic but they work wonderfully -- my poerformance problems have alway been elsewhere :-)

Check out the "Canonical Hostnames" section of [httpd.apache.org ] -- all you need to do is be able to distinguish a "directory" request from a "page" request. So for example if all pages end with some extension like .html, .jsp, .whatever you could rewrite any requests that didn't have this pattern by tacking a slash onto the end, like:


RewriteCond!^/.*\..*$
RewriteRule ^(.*)$ $1/

The condition says "if the URL doesn't have a dot somewhere in the middle" and the rule says "rewrite the entire URL, the part in the parens, to one that has a slash on the end. If you can distinguish between regular URLs and ones that are probably directories more simply, then you can just use a rule. You could probably do this without the condition but I would have to test that, e.g.


RewriteRule!^(/.*)\..*$ $1/

I am sure someone can tell us if this latter rule would work.

jdMorgan

11:22 pm on Dec 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



twisty80,

Welcome to WebmasterWorld!

Your server should do this already, due to the action of Apache mod_dir. If it's not doing it automatically, then you probably have UseCanonicalName on set in httpd.conf. Either turn it off, or change the canonical name to include the trailing slash.

Jim