Forum Moderators: phranque
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.
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.
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]
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.
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.
*** 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?
RewriteRule ^pics/Wooden\ helicopter\ \(yellow\)\.php$ http://example.com/products.php?product_id=45 [R=301,L]
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