Forum Moderators: phranque
My company has a page we are optimizing.
Originally the URL read
http://www.example.com/index.html?p=products&s=sports
We re-wrote it to
http://www.example.com/products/Sports.html
We want to redirect it to the new URL so we dont lose our ranking in
google.
We want it to read.
http://www.example.com/products/Browse_Sports_Posters/Sports-Posters.html
Which Works.... BUT IT SHOWS....
http://www.example.com/products/Browse_Sports_Posters/Sports-Posters.html?p=products&s=Sports
What Am I doing wrong. In the HTACCESS I HAVE...
redirect 301 /products/Sports.html http://www.example.com/products/Browse_Sports_Posters/Sports-Posters.html
[edited by: jdMorgan at 10:36 pm (utc) on Aug. 25, 2008]
[edit reason] example.com [/edit]
First, your rewrite is apparently executing before your redirect, and second, you'll need to use mod_rewrite to do this redirect and explicitly clear the query string attached to the URL. It's likely that the "rewrite" described in your post isn't quite right, either.
Basically, it appears your redirect is "exposing" the rewritten URL's query string. In order to control the execution order of your redirect and your rewrite, both must be coded using the same module -- you cannot mix mod_alias and mod_rewrite directives and be sure which will execute first.
For an in-depth treatment of this subject, see this thread [webmasterworld.com] in our forum library.
Jim
How Do I avoid doing this 1000 times.
RewriteRule ^products/Sports.html$ http://www.example.com/products/Browse_Sports_Posters/Sports-Posters.html$1 [R=301]
RewriteRule ^products/Movies.html$ http://www.example.com/products/Browse_Movies_Posters/Movies-Posters.html$1 [R=301]
RewriteRule ^products/Animals.html$ http://www.example.com/products/Browse_Animals_Posters/Animals-Posters.html$1 [R=301]
RewriteRule ^products/College.html$ http://www.example.com/products/Browse_College_Posters/College-Posters.html$1 [R=301]
I know how to rewrite from a ugly variable. Just unsure of this.
[edited by: jdMorgan at 4:18 pm (utc) on Aug. 29, 2008]
[edit reason] use example.com please [/edit]
RewriteRule ^products/([^\.])\.html$ http://www.examplecom/products/browse-$1-posters/$1-posters.html? [R=301,L]
I would also go with all lower case URLs. Mixed case URLs can cause all sorts of issues.
Never use spaces or underscores in URLs. They cause too many issues. Use hyphens or dots instead.
[edited by: jdMorgan at 4:19 pm (utc) on Aug. 29, 2008]
[edit reason] example.com [/edit]
However. I have narrowed down to these 2 rules. are they written right? its just ignoring my second rule. when I remove the first rule it stops working
RewriteRule ^products/([^/]*)\.html$ /?p=products&s=$1 [L]
RewriteRule ^products/([^\.])\.html$ http://www.example.com/products/Browse-$1-Posters/$1-Posters.html? [R=301,L]
I tried these variation and it did not work.
RewriteRule ^products/([^\.])\.html$ /products/Browse-$1-Posters/$1-Posters.html? [R=301,L]
RewriteRule ^products/([^/]*)\.html$ /products/Browse-$1-Posters/$1-Posters.html? [R=301,L]
Got any help for me?
[edited by: jdMorgan at 4:19 pm (utc) on Aug. 29, 2008]
[edit reason] use example.com please [/edit]
If it's not clear, mod_rewrite looks at the URL that is requested by the browser (either by typing in an address or by clicking on a link), and then if it matches the regular expressions pattern in the left side of the rule, either rewrites the request to the filepath on the right, or redirects to the URL on the right, depending on the syntax used in the rule (redirect or rewrite). However, it is differences in the requested URLs that allow mod_rewrite to determine which (if any) rule should be applied, based on the regex pattern you write in the rule.
And as g1smd stated above, your redirects should almost always be placed before your rewrites, and these two rules are in revers order according to that recommendation.
In order to get a solution, you do not need to post almost-correct code here. But you do need to post an absolutely correct and complete description of what variations of URLs are to be rewritten and/or redirected to what filepath and/or new URL. Without that, we may give you a 100%-correct answer to the wrong question!
Jim
RewriteRule ^products/([^/]*)\.html$ /?p=products&s=$1 [L]
RewriteRule ^products/([^\.])\.html$ http://www.example.com/products/Browse-$1-Posters/$1-Posters.html? [R=301,L]
It ignored what I wrote.
Do you think its the difference between
([^/]*) vs ([^\.]) ?
[edited by: jdMorgan at 5:38 pm (utc) on Aug. 29, 2008]
[edit reason] Please use example.com [/edit]
It is likely that what was intended was "([^.]+)" meaning, "match *one or more* characters not equal to a period."
However this still leaves the case where most URLs that match the first rule will also match the second, since the first rule's pattern will match anything without a slash in it, leaving only slashed URLs for the second rule to handle. This may in fact be what you intended, but we here don't know that.
But if not, then either you need to make the pattern more specific, or you need to change the URLs themselves, so they are different enough so that different patterns can be used to tell mod_rewrite which rule should be applied.
Again, you must correctly and completely describe your requirements --tell us what you *intend* these rules to do-- before any correct solution can be offered.
It is a fact that once learned, writing mod_rewrite code is very easy and reading it is fairly easy. But coming up with correct and complete requirements before coding is *always* a challenge, no matter how long you've been coding mod_rewrite...
Jim
We have URLS already in google but they are not great so we want to redirect them to the new string so that google updates the URL with the 301 Redirect.
The problem persist because I have the rewrite rule that was first implemented(and works). Now I want to simply apply a rewrite rule to rewrite the Indexed URL that has been already been re-written.
(that should make sense.)
# Externally redirect /products/<product_name>.html URLs to
# products/Browse-<product_name>-Posters/<product_name>-Posters.html
# URLs and clear any appended query string
RewriteCond $1 !^Browse-[^.\-]+-Posters/[^.\-]+-Posters$
RewriteRule ^products/([^./]+)\.html$ http://www.example.com/products/Browse-$1-Posters/$1-Posters.html? [R=301,L]
# Internally rewrite new "products/Browse"-format URLs to script file
RewriteRule ^products/Browse-([^.\-]+)-Posters/[^.\-]+-Posters\.html$ /index.html?p=products&s=$1 [L]
It also assumes that the product_name itself will not contain hyphens or slashes. If this could happen, then the patterns will need to be made more complex to accommodate them.
Jim
[edited by: jdMorgan at 7:21 pm (utc) on Aug. 29, 2008]