Forum Moderators: phranque

Message Too Old, No Replies

Using the HTACCESS 301 Redirect. just not the way it should

I dont want to lose rank. I can redirect but extra variables the show

         

acimag

8:58 pm on Aug 25, 2008 (gmt 0)

10+ Year Member



First off this is my first post so I hope this works out. There is a lot of geeks here just like me so already I feel at home. Lets get into it.

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]

jdMorgan

10:35 pm on Aug 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two problems:

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

g1smd

11:44 pm on Aug 25, 2008 (gmt 0)

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



Use RewriteRule (and [R=301] for redirects) rather than Redirect, and make sure that all redirects are placed before any internal rewrites.

acimag

1:16 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



Thank you so much. I had the order wrong. Thanks for the help guys.

acimag

1:32 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



Now my next question:

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]

g1smd

6:40 pm on Aug 26, 2008 (gmt 0)

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



Something like this:

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]

acimag

2:19 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



Hey g1 thanks for all the help.

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]

jdMorgan

4:17 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those two rule patterns are very, very similar, and in most cases, any URL that would match the first pattern would also match the second. Since you know what URLs you intend to rewrite (first rule) or redirect (second rule), you will need to tell us (and tell mod_rewrite by using a more-specific pattern) which is which.

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

acimag

5:30 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



I switched the Order:

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]

jdMorgan

5:44 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first pattern "([^/]*)" means "match *any number* of characters (including zero) not equal to a slash."
The second pattern "([^\.])" means "match *one* character not equal to a literal period."

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

acimag

6:23 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



To let you know EXACTLY what I am doing. This rule will apply to the SEO portion.

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.)

jdMorgan

7:17 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well again, you cannot rewrite/redirect one (the same or similar) incoming requested URL to two different destinations at the same time. So if I understand your requirements, you'll need:

# 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]

This assumes that the script is named "index.html" and resides in the Web root ("home page") directory of the site. That script will probably also need to output server-relative or canonical URLs (and not page-relative URLs) when producing on-page links to other pages, image/multimedia files, CSS, or included JavaScripts. Otherwise, the browser will resolve these page-relative links based on the "virtual" /products/Browse-XYZ-Posters/ directory, and the result will be incorrect URLs for those resources.

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]

g1smd

11:38 pm on Aug 29, 2008 (gmt 0)

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



I see now that I missed at least a + out of my example code. Grrr.

([^\.]+)\.

acimag

1:20 pm on Sep 2, 2008 (gmt 0)

10+ Year Member



Thanks guys. That did the trick.

I gotta read more into this. Its so powerful. I really appreciate everyones time. This is great words to index for SEO. I hope many people learn from this thread.