Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 redirects to uppercase first letter of every word

404 Error due to Capital Letters in URL

         

StellarBytes

2:17 am on Nov 15, 2011 (gmt 0)

10+ Year Member



Hello people.

Hoping someone can help me here, this is driving me insane!

I use CS-Cart, but believe this is an .htaccess issue rather than an issue with the cart itself.

The old categories URL's (from Interspire cart) were in this format, both uppercase and lowercase worked successfully.

domain.co.uk/categories/category-name/

OR
domain.co.uk/categories/Category-Name/


The new format, which only works in lowercase:

domain.co.uk/category-name/


I have set up the redirect like so, note the redirected URL is all in lowercase:

redirect 301 /categories/category-name http://www.domain.co.uk/category-name/


However, these redirects, when I click the relevent result in Google, kind of redirects successfully, but produces a 404 because the first letter of every word is in Capitals (Uppercase), like below, note it is only the 'category-name' part which is in uppercase, which I can't explain nor find any solutions for:

http://www.domain.co.uk/Category-Name/


This produces a 404 not found error. If I change this to the following, the page loads successfully, thus, the capital letters are definitely the problem.

http://www.domain.co.uk/category-name/


OK, so here's my .htaccess file which includes various gZip compressions and SmartOptimizer, amongst other things:



Options +FollowSymLinks
RewriteEngine on

DirectoryIndex index.html index.php

<IfModule mod_expires.c>
<FilesMatch "\.(gif|jpg|jpeg|png|swf|css|js|html?|xml|txt|ico)$">
ExpiresActive On
ExpiresDefault "access plus 10 years"
</FilesMatch>
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\.(js|css))$ smartoptimizer/?$1

<IfModule mod_expires.c>
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.(js|css|html?|xml|txt))$ smartoptimizer/?$1
</IfModule>

<IfModule !mod_expires.c>
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.(gif|jpg|jpeg|png|swf|css|js|html?|xml|txt|ico))$ smartoptimizer/?$1
</IfModule>
</IfModule>
<FilesMatch "\.(gif|jpg|jpeg|png|swf|css|js|html?|xml|txt|ico)$">
FileETag none
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/cgi-bin/.*
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|php|html)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
<IfModule mod_deflate.c>
<FilesMatch "\.js$">
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*)\.js$ $1\.js.gz [L]
ForceType text/javascript
</FilesMatch>
</IfModule>
<IfModule mod_deflate.c>
<FilesMatch "\.js\.gz$">
ForceType text/javascript
Header set Content-Encoding gzip
Header set Vary Accept-Encoding
</FilesMatch>
</IfModule>

redirect 301 /categories/root-category/sub-category/ http://www.domain.co.uk/root-category/sub-category/



Can anybody spot the problem, which causes the redirect to go to
http://www.domain.co.uk/Root-Category/Sub-Category/
and NOT
http://www.domain.co.uk/root-category/sub-category/
as it 'should'?

Note that because Interspire's URL's are generated from the Product/Category/Page name, the URL's were most often generated as the cateogory name reads, 'Some Category Page' became .co.uk/categories/Some-Category-Name/, whereas CS-Cart ONLY seems to handle - .co.uk/categories/some-category-name/ in lowercase.

Thanks in advance!

g1smd

5:52 am on Nov 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Multiple problems, the same ones I type out every day:

Add [L] flag to every rule.

Trailing uncaptured .* can be removed.

NEVER begin a pattern with .* or (.*). Use a pattern that does not force thousands of back off and retry trial matches.

Rule target $1\.js.gz should be /$1.js.gz unless you want your server hacked by path injection.

Literal periods in RegEx patterns should be escaped. Those in literal URLs should not.

Never mix Redirect and RewriteRule in the same site. Use RewriteRule for all of the rules.

RewriteEngine On should appear once, not three times.

List RewriteRules which redirect before those that rewrite. That is, your very last rule "Redirect" should be converted to a RewriteRule and listed near the beginning of the file.

You'll likely need to add another rule to fix the upper case problem. At the very beginning of the .htaccess file rewrite upper case requests to a special PHP file that has a script which fixes the casing and sends the correct 301 HEADER out. There's code for this is several previous threads.