Forum Moderators: phranque
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...
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]
[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