Forum Moderators: phranque

Message Too Old, No Replies

What does this htaccess do?

         

happystinky

9:06 am on Aug 19, 2005 (gmt 0)

10+ Year Member



Can anyone help me figure this out?

The following codes put in the same file messes everything up and I get 404 errors. They just don't play well together I guess but there is hopefully a way to correct the problem.

First the original code - works fine by itself:

options all
RewriteEngine on
RewriteBase /
RewriteRule shop /cgi-bin/cart.pl

Then when we add this (which replaces spaces in the url with a hyphen) it also works fine:

Options +FollowSymlinks

RewriteCond %{REQUEST_URI} ^(.*)\ (.*)$
RewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
RewriteCond %{ENV:space_replacer}!^$
RewriteCond %{ENV:space_replacer}!^.*\ .*$
RewriteRule ^.*$ %{ENV:space_replacer} [R=301,L]

But when we add this bit to it everything breaks:

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*) index.php

Each part is important but if we could only figure out why they are battling eachother. I'm not quite sure what the last one does. At first I thought it was just for Mambo so that rather than sending 404s to custom 404 pages, it just sends all non-existent pages to the index.php page. But it could also just be part of Mambo OpenSource that creates cleaner looking URLs?

Any help will be much appreciated. We've been racking our brains for hours on this...

jdMorgan

2:34 pm on Aug 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect there's something wrong with the last rule, perhaps because {REQUEST_FILENAME} is not returning the value you expect. Check your server error log to find the path that the server is trying to use when it gets the 404. It is sometimes necessary to use %{DOCUMENT_ROOT}%{REQUEST_FILENAME} to get the full path; Your error log will show this.

Also, what is the result if you leave out the space-remover code temporarily?

That space-remover code is a bit bloated, and will require one redirect per space, which will be quite inefficient for more that one space. The following may be a better approach:


# Replace any spaces and restart in case there are more
RewriteRule ^([^\ ])*\ (.*)$ $1-$2 [E=space_replaced:yes,N]
#
# If one or more spaces have been replaced, externally redirect to new URL
RewriteCond %{ENV:space_replaced} ^yes$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

However, I suggest you get the whole thing working one step at a time, before adding complexity.

[added] The last rule rewrites any request for a resource that does not exist to index.php in the current directory. Be sure that index.php exists in the current directory, otherwise, the code will cause an 'infinite' rewrite loop. [/added]

Jim