Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!


Sponsored Content

 

 
 

Photo
- - - - -

Newbie! Changing Shopping Carts


  • Please log in to reply
23 replies to this topic

#16 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 04 February 2007 - 08:51 AM

You're running into the problem because mod_rewrite does not natively support character replacement. It's easy to do it at the file level with PHP, ASP and the like, but mod_rewrite just doesn't support character replacement like they do. You can do it with a rewrite rule, but it's a bit of a kludge. In fact, if you use the N flag you can even instruct the server to restart mod_rewrite after it detects and replaces the first space. This way it'll keep iterating through itself until all spaces have been replaced.

A bit of a caution is in order here...

The space character is one of those special charcters in mod_rewrite. So anytime you're trying to detect on you have to first escape it with a backslash character so that it is treated literally. In your example above, you would need to change
CODE
RewriteCond %{REQUEST_URI} ^(.*) (.*)$

so that it reads
CODE
RewriteCond %{REQUEST_URI} ^(.*)\ (.*)$


As a second caution, you'll want to place any space detecting rewrite as close to the top of your .htaccess file as possible. If it won't interfere with other rewrite rules you'll want it at the very top. You definitely want it before you start tyring to come up with the final url.

I've never done this with query strings, but I have had to do it a time or two when someone was moving a site from a IIS server to a *nix server where there were file names that had a space in them, which isn't allows on a *nix server. The theory is the same, you're just working on a different part of the url string. The following is what I used in those cases, so should get you pretty close. (I've added some line spacing and comments to help point out what section is doing what.)

CODE
Options +FollowSymlinks
RewriteEngine on

# Replace spaces with hypens
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=replace_space:yes,N]

#Redirect replace_space positives to the updated URL
RewriteCond %{ENV:replace_space} yes
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]


In the above the first RewriteRule and anything that preceeds it will be processed over and over until no other spaces exist in the URL string. This is why it's important to have your space detection as close as possible to the top of your .htaccess file.

Does that help?

I'll try to find some time before the Super Bowl Party preperations begin today to see if I can't construct a little test for spaces in query strings. I don't think it should be too hard, but not having done it before one never knows. I'm not sure how much spare time I'll have to play with it today though.

#17 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 04 February 2007 - 11:03 AM

Okay, I found a little bit of time. And thankfully remembered something I'd previously forgotten before I started on the rules.

What I'd forgotten is that mod_rewrite doesn't actuall see variables, thus gives you no way to manipulate them. It'll let you match against query strings when you use %{QUERY_STRING} but never actually sees the variable value. Saves some time with having to fight with it to have remembered this obscure bit of tid while I was slicing and dicing some veggies for the appetizer tray. lol.gif

Anyway, just because %{QUERY_STRING} won't work that doesn't necessarily mean we're out of gas. It just means we need to find another way to get to the variable value information that will be in the category variable. And thankfully we should be able to use mod_rewrite's %{THE_REQUEST} to get to it.

It's stil a bit of a kludge, but the following works on my servers (with standard log output) for Category Names that from no spaces up to a maximum of 3 spaces. Again, this is written expressly to be placed in the cgi-bin folder to keep it from interferring with other rewrites you may have in your root level .htaccess file. The #'s are comments to let you know what the following lines are doing.

CODE
Options +FollowSymlinks
RewriteEngine on
# Look for 3 spaces in the category name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)store\.cgi(.*)category=(.*)\ (.*)\ (.*)\ (.*)\ HTTP/
RewriteRule ^(.*) http://www.domain.com/store/%3-%4-%5-%6? [R=301,L]
# Look for 2 spaces in the category name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)store\.cgi(.*)category=(.*)\ (.*)\ (.*)\ HTTP/
RewriteRule ^(.*) http://www.domain.com/store/%3-%4-%5? [R=301,L]
# Look for 1 space in the category name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)store\.cgi(.*)category=(.*)\ (.*)\ HTTP/
RewriteRule ^(.*) http://www.domain.com/store/%3-%4? [R=301,L]
# No spaces in category name
RewriteCond %{QUERY_STRING} category=([^&]+) [NC]
RewriteRule store.cgi$ http://www.domain.com/store/%1? [R=301,L]


Give that one a whirl and see if it does what you want.

There's still no Case Correction going on. That one is all but impossible using mod_rewrite since it involves 27 different rules and looping, which can get really load intensive. If you need to do that, I would do it in the destiniation file instead of with mod_rewrite. Much safer that way.

#18 RJC

RJC

    HR 2

  • Members
  • PipPip
  • 12 posts

Posted 04 February 2007 - 01:25 PM

It's not working (but at least it's not generating a 500 error like my amatuerish attemps!!)

You also have to excuse my lack of knowledge in this area..I'm not a programmer..or anything close to it. I do see now what you mean about the mixed case. My old URLs used a lot of mixed case, mostly capital first letters where you would usually expect them.."Dogs", "Dogs and Cats" (words like and, of, etc. normally lowercase)...I AM able to change my new URLs to anything I wish, so I capitalized a category name, and presto...the original code you gave me worked like a charm on a category it hadn't worked for before (not sure why some would work and some wouldn't..maybe it was how I typed the search term in goooogle to begin with)..so instead of watching the Super Bowl, I think I'll be changing all of my new categories to the same case they formerly used, as the one worders will redirect correctly at least for now. I hope that's the right thing to do? I do have some hard-coded links using strictly lowercase, but they seem to still work even after I change the rewrite URLs to mixed case. God this is confusing..

I'll also mess with some of the space removing ideas and see if by some miracle I can figure it out!

#19 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 04 February 2007 - 05:00 PM

Do you have access to your raw log files RJC?

The above is for a standard Apache log format, but yours may be tweaked a bit. Having a look at a hit or two should tell us if the THE_REQUEST lines need to be tweaked for a different format.

Or you may want to grab a copy of the freebie WebBug software. It'll show you exactly what URL things are being sent to with the redirects and help to sort out any niggling issues. Make sure to check off HTTP Ver. 1.1 in the top right corner if you use that one. Then just feed it one of the old URLs that isn't working properly and see if something is leading off to a different URL address than you want it to.

#20 RJC

RJC

    HR 2

  • Members
  • PipPip
  • 12 posts

Posted 04 February 2007 - 06:23 PM

This might help...

Webbug says something like this (edited):
CODE
HTTP/1.1 301 Moved Permanently
Date: Sun, 04 Feb 2007 23:18:08 GMT
Server: Apache/2.0.52 (Red Hat)
Location: [url=http://www.domain.com/store/Cats%2520Dogs]http://www.domain.com/store/Cats%2520Dogs[/url]
Content-Length: 335
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>


I don't know what %2520 is, but that appears to be what it is changing the space into?

#21 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 05 February 2007 - 09:15 AM

%2520 is an encoded ampersand character I believe. No space, no anything else, just an Ampersand. Which would mean the old url category should look like Cats&Dogs

If my hunch is correct, the following added to your root level .htaccess file should correct it.

CODE
# Replace Ampersand
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /store/(.*)\%2520(.*)\ HTTP/
RewriteRule ^(.*) http://www.domain.com/store/%1&%2 [R=301,L]


Note: This one goes in a in the root level .htaccess instead of the cgi-bin .htaccess we were working with before.

Note #2: Depending upon your server configuration you may need to tweak the rewrite line so that /store/%1&%2 already has the ampersand encoded. There's no way I can test it, but if you need that you should be able to use
CODE
%1&amp;%2

instead of what's above.

#22 RJC

RJC

    HR 2

  • Members
  • PipPip
  • 12 posts

Posted 05 February 2007 - 11:44 AM

Before I try this - I know my old category used an ampersand, not in the category name, but in the query string, so it looked something like this:

CODE
http://www.domain.com/cgi-bin/store.cgi?user_action=category&category=Cats%20and%20Dogs


I just double checked to be sure - definitely no ampersand in category names - just in the query string.

Should I still try the above anyway?

#23 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 05 February 2007 - 12:04 PM

That's not what the redirect indicates RJC.

CODE
Location: http://www.domain.com/store/Cats%2520Dogs


It doesn't include the word "and" in there anywhere. And if there were two spaces (eg category=Cats<space>and<space>Dogs) the resulting redirect should have been

CODE
Location: http://www.domain.com/store/Cats-and-Dogs


So you've got something unusual happening somewhere. You'll need to figure out where that ampersand character is coming from. It's there somewhere.

#24 RJC

RJC

    HR 2

  • Members
  • PipPip
  • 12 posts

Posted 05 February 2007 - 12:12 PM

Oh, no no..I'm sorry..of course these are not REAL categories..I was just trying to illustrate the spaces..

Most categories (such as the one I pasted from WebBug) have only 2 words - Cats%20Dogs...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users