Forum Moderators: phranque

Message Too Old, No Replies

Redirect url with spaces

         

ChocolateLover

11:56 am on Apr 2, 2009 (gmt 0)

10+ Year Member



I can't seem to get this rule to work.

The URL I want to redirect is
pics/Wooden helicopter (yellow).php
and I want to redirect it to
products.php?product_id=45

I'm using
RewriteRule ^pics/Wooden\shelicopter\s(yellow).php products.php?product_id=45 [L]
and I've tried
RewriteRule ^pics/Wooden\ helicopter\ (yellow).php products.php?product_id=45 [L]

Any help would be appreciated, thanks.

g1smd

12:11 pm on Apr 2, 2009 (gmt 0)

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



I am guessing that you don't want a redirect, but instead need a rewrite.

The code you have supplied is for a rewrite.

Your problem shows why the advice to "never use spaces or underscores in URLs" is regularly given here.

Can you arrange this so that you use hyphens in the links on your page? You'll find this a lot easier to deal with.

ChocolateLover

12:23 pm on Apr 2, 2009 (gmt 0)

10+ Year Member



I completely agree about the spaces etc but these are URLs that are already out in Cyberspace (and getting hits) and I'm in the process of converting to more search engine friendly URLs.

I don't quite understand your reply sorry, I guess I'm not identifying the spaces properly in the existing URL.

Any suggestions appreciated.

[edited by: ChocolateLover at 12:24 pm (utc) on April 2, 2009]

g1smd

1:23 pm on Apr 2, 2009 (gmt 0)

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



I would change to hyphens within the links on your site, and make those the format that people 'see' and 'use'.

I would use those with a rewrite to fetch the content for those requests.

I would then set up a redirect so that requests with spaces in are redirected to make a new request for the new URL with a hyphen.

In that way, people following old links will still get to see the content, whilst being directed to change to the new URL system.

ChocolateLover

1:48 pm on Apr 2, 2009 (gmt 0)

10+ Year Member



Yes, ultimately that's what I will do, but as the URLs already exist with spaces I need to work out how to deal with the spaces in the rewrite, which is what I'm having problems with.

So, just to clarify:
I am working on a website that has URLs that have been indexed by search engines etc. Many of these URLs have spaces.

In the first instance I want to redirect the URLs that no longer exist.
Example pics/Red car.php to go to pics/solar-racer.php

This is my rule
RewriteRule ^pics/Red car.php pics/solar-racer.php [L]

Leaving a space between Red and car gives me:
The server encountered an internal error or misconfiguration and was unable to complete your request.

I've read:
"\ " is a way of expressing spaces in mod rewrite
and
"\s" is a way of matching spaces

I've tried
RewriteRule ^pics/Red\scar.php pics/solar-racer.php [L]
but this rule doesn't seem to pick up the pics/Red car.php URL at all.

I will look at replacing all spaces with hyphens but need to sort this problem first.

Thanks for your help.

g1smd

2:05 pm on Apr 2, 2009 (gmt 0)

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



*** In the first instance I want to redirect the URLs that no longer exist. ***
*** Example pics/Red car.php to go to pics/solar-racer.php ***

*** This is my rule ***
***

RewriteRule ^pics/Red car.php pics/solar-racer.php [L]
***

URLs that no longer exist should be redirected, but your code here is not for a redirect. You have it coded as a rewrite.

With a rewrite, you are letting the user-agent continue to use the old URL to access the content. You are returning '200 OK' for that request, and thereby causing two problems. One is that you are creating a Duplicate Content URL for that content, and the other is that you are not forcing user agents to update the URL they use to request the content, and so they will continue forever to use the 'wrong' URL.

Change that rule to be a redirect, using a very similar RewriteRule. The new rule will contain the full domain name in the target URL, and have [R=301,L] at the end.

So, you redirect people to a new URL, and then you use a rewrite to connect the new URL request to the internal server location to get the content from.

.

You need to be very clear in your own mind as to the differences between a redirect and a rewrite, the differences in what they do, the differences in how they are coded, and the appropriate time to choose one or the other.

Otherwise you could be providing the right solution to the wrong problem.

.

As for the specific problem with spaces, maybe are they encoded to %20 and therefore not matching? Maybe the pattern needs to be enclosed in quotes, or surrounded with brackets?

jdMorgan

3:27 pm on Apr 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should work if indeed you want an external redirect instead of an internal rewrite:

RewriteRule ^pics/Wooden\ helicopter\ \(yellow\)\.php$ http://example.com/products.php?product_id=45 [R=301,L]

Do you have other working rules?

Do you have other rules preceding this new one that would also match that "pics/Wooden" URL, and take action before this new rule can be invoked?

Did you preface your mod_rewrite code with the Options +FollowSymLinks and RewriteEngine on directives needed to enable mod_rewrite?

The advice about not using spaces is sound. In fact, you really shouldn't use any characters in the URL-path parts of your URLs which are defined as 'reserved' in the HTTP/1.1 protocol. If you do, they will have to be encoded for transmission (e.g. space becomes %20). And using mixed-case URLs can be a true nightmare when switching between Apache and IIS servers, because Apache is case-sensitive and IIS is not. Therefore, I recommend using only a-z, 0-9, and hyphens in URL-paths. However, the job at hand is to get your immediate problem resolved, so we can defer discussion of these other issues until later.

Jim

ChocolateLover

3:40 pm on Apr 2, 2009 (gmt 0)

10+ Year Member



Thanks so much, this worked.

Yes, I had other rules that worked, I wasn't escaping the characters properly in this rule.

I hear what you're saying about spaces etc.

Thanks again.