Forum Moderators: phranque

Message Too Old, No Replies

301 and new indexing rule

301 new indexing rule

         

fastfriend

6:22 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



Continued from [webmasterworld.com...]

I have done the following rewrite rules for my existing website as

RewriteRule ^C([^/]+)/([^/]+).html /category.php?cid=$1&category=$2 [L]
RewriteRule ^I([^/]+)/([^/]+).html /item.php?iid=$1&item=$2 [L]

More than 1000 PHP pages with query strings are already indexed in Google.

Now, I am confused with the following:

1) I want that my all indexed pages now updated with new .html pages. What Rewrite Rule or Conditions are required ?

2) All .html pages are working with www and without www. Now I want every page only work with WWW. What Rewrite Rules or Conditions are required.

Thanks

[edited by: jdMorgan at 9:52 pm (utc) on Oct. 7, 2008]
[edit reason] Linked to previous thread. [/edit]

g1smd

6:37 pm on Oct 7, 2008 (gmt 0)

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



This thread pretty much has a complete template for what you need to do:

[webmasterworld.com...]

You need a series of redirects, one to fix each of the things you mentioned.

Read the comments on each line of code; it is all explained line by line.

There is some stuff at the beginning that fixes some common malformed requests. Using that code is optional.

fastfriend

8:08 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



I have applied the following

RewriteRule ^C([^/]+)/([^/]+).html /category.php?cid=$1&category=$2 [L]
RewriteRule ^I([^/]+)/([^/]+).html /item.php?iid=$1&item=$2 [L]

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

It seems working fine with WWW.

There are 2 questions now including 1 issue I noticed:

1) All URLs working fine with WWW. But if I remove WWW from the newly rewrite .HTML page, e.g. http://example.com/Cnnn/cat-name.html it becomes original PHP with WWW with all query strings.

2) Does it serves the 301 purpose for Google so all PHP references would be updated by .HTML pages ?

Thanks for co-operation

[edited by: jdMorgan at 9:25 pm (utc) on Oct. 7, 2008]
[edit reason] Please use example.com only. [/edit]

jdMorgan

8:53 pm on Oct 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your two rules are reversed. Put your redirects in order from most-specific to least-specific, followed by your internal rewrites, again in order from most-specific to least-specific.

You now need to add redirect rules which examine the client request, and redirect only direct client requests for the dynamic URLs to the new static URLs. The process is explained in the thread that g1smd cited above: See the third rewriterule in that thread for a specific example of what you need to do.

Jim

[edited by: jdMorgan at 8:54 pm (utc) on Oct. 7, 2008]

fastfriend

9:30 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



Are you talking about this rule

# Rewrite URL request: www.example.com/345/1234567 to internal
# path: /index.php?cat=345&art=1234567 to serve content:
RewriteRule ^([0-9]{3})/([0-9]{7})$ /index.php?cat=$1&art=$2 [L]

For rules are reversed, do you mean these should be

RewriteRule ^I([^/]+)/([^/]+).html /item.php?iid=$1&item=$2 [L]
RewriteRule ^C([^/]+)/([^/]+).html /category.php?cid=$1&category=$2 [L]

jdMorgan

9:53 pm on Oct 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please read what I wrote carefully. I said put your redirect (the one with [R=301,]) first.

Jim

fastfriend

11:55 am on Oct 10, 2008 (gmt 0)

10+ Year Member



So far now-www to WWW is working fine and Re-wrtie Rules are working fine too.

Now I want to implement that if someone comes from Google indexed page which is
http://www.example.com/category.php?cid=123&category=test

to redirect to
http://www.example.com/C123/test.html

I have got the following from the thread 3753656:

#External URL Format: www.example.com/345/1234567
#Internal Server Path: /index.php?cat=345&art=1234567

# Redirect two-parameter-based index.php多tml? or / URL request
# (with parameters in any order) to folder-based URL format, and
# force www to always be in URL:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.(php多tml?))?(\?[^\ ]*)\ HTTP/ [NC]
RewriteCond %{QUERY_STRING} &?cat=([0-9]{3})&?
RewriteCond %1>%{QUERY_STRING} ^([^>]+)>([^&]*&)*art=([0-9]{7})&?
RewriteRule ^(index\.(php多tml?))?$ http://www.example.com/%1/%3? [R=301,L]

I believe that the above code would solve the issue. I have changed this to my needs as


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(category\.(php多tml?))?(\?[^\ ]*)\ HTTP/ [NC]
RewriteCond %{QUERY_STRING} &?cid=([0-9])&?
RewriteCond %1>%{QUERY_STRING} ^([^>]+)>([^&]*&)*category=([*asterik])&?
RewriteRule ^(category\.(php多tml?))?$ http://www.example.com/%1/%3? [R=301,L]

But it does not work. And the user comes from Google or type himself http://www.example.com/category.php?cid=123&category=test work as it is and NOT http://www.example.com/C123/test.html

What is wrong ?

[edited by: fastfriend at 12:36 pm (utc) on Oct. 10, 2008]

g1smd

11:58 am on Oct 10, 2008 (gmt 0)

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



Please put the 'code' tags round each block of code and tick the "disable smilies for this post" box.

You have 60 minutes from initial posting to click the "owner edit" button to be able to do that.

fastfriend

12:37 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Inserted 'Code' Tags and similies are disabled.

jdMorgan

2:05 pm on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Redirect direct client requests for /category.php?cid=123&category=test or /category.php?category=test&cid=123 to http://www.example.com/C123/test.html

# Get the entire category.php query string to %1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /category\.php\?([^\ ]+)\ HTTP/ [NC]
# Retain the category query string in %1, get cid to %3
RewriteCond %1>%1 ^([^>]+)>({^&}+&)*cid=([0-9]+)&?
# Move cid from %3 to %1, then get category to %3
RewriteCond %3>%1 ^([^>]+)>({^&}+&)*category=([^&]+)&?
RewriteRule ^category\.php$ http://www.example.com/C%1/%3.html? [R=301,L]

Untested -- No warranty.

Test it, and if it works, reproduce it for the "items" URLs, extracting/testing the "iid" and "item" parameters, and redirecting to "http://www.example.com/I/%1/%3.html?"

Jim

g1smd

3:27 pm on Oct 10, 2008 (gmt 0)

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



In your original code, the
&?cid=([0-9])&?
would only match a single-digit cid value (like C3 or C7 only); and I see jd also noticed that.

The

 {3}
in the original example code was there to specify the number of digits (the length) to match, as in that example it was always a fixed length.

If it is not a fixed length, then use a

+
meaning "one or more digits".

fastfriend

6:29 am on Oct 11, 2008 (gmt 0)

10+ Year Member



It simply does not work.

fastfriend

7:30 am on Oct 11, 2008 (gmt 0)

10+ Year Member



Anyway, Thanks JD and G1.

Can you explain what does %{THE_REQUEST} ^[A-Z]{3,9} means ? Especially {3,9}.

What I understand is A-Z means only Alpha Characters and {3,9} may be a limit.

jdMorgan

1:54 pm on Oct 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please refer to the documents cited in our Forum Charter for basic information.

Thanks,
JIm

g1smd

8:09 pm on Oct 11, 2008 (gmt 0)

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



"Doesn't work" is far too wide a description to convey any useful meaning.

fastfriend

8:36 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



since I dont get any ERROR I would say it simply does not work.

And the user comes from Google or type himself http://www.example.com/category.php?cid=123&category=test work as it is and NOT http://www.example.com/C123/test.html

jdMorgan

8:38 pm on Oct 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup, but there's a typo in the second and third RewriteConds. Should be:

# Get the entire category.php query string to %1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /category\.php\?([^\ ]+)\ HTTP/ [NC]
# Retain the category query string in %1, get cid to %3
RewriteCond %1>%1 ^([^>]+)>[b]([^&]+[/b]&)*cid=([0-9]+)&?
# Move cid from %3 to %1, then get category to %3
RewriteCond %3>%1 ^([^>]+)>[b]([^&]+[/b]&)*category=([^&]+)&?
RewriteRule ^category\.php$ http://www.example.com/C%1/%3.html? [R=301,L]

Sometimes you should hit the Shift key, and sometimes, you shouldn't...

Jim

fastfriend

9:08 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Thanks Jim

It now gives 404 Page not Found ERROR .

g1smd

9:11 pm on Oct 11, 2008 (gmt 0)

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



As for the
{3,9}
part, it means must be "3 or more" and "9 or less".

You can specify an exactly length, like

{12}
, or a range.

fastfriend

9:32 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Sorry, my mistake.

The code is working fine now.

Now a little tweaking required.

It works fine if the category has a single word title e.g. test

but when I have a category name like

category=math test
category=math%20test (indexed in google)

it becomes
math%2520test.html

how to get rid off %2520.

fastfriend

9:47 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



OK

I use [NE] flag as
RewriteRule ^category\.php$ http://www.example.com/C%1/%3.html? [R=301,NE,L]

so %2520 now become %20.

Now I want to change %20 into - . Any help ?

jdMorgan

12:58 am on Oct 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



change the script so it generates links with "category=math-test" instead of "category=match test"

HTTP *requires* that spaces be sent as %20, so you need to stop them being sent at the source.

Jim

fastfriend

6:48 pm on Oct 12, 2008 (gmt 0)

10+ Year Member



I can do it from the script, but I have a question here:

Since the page is already indexed as category.php?cid=123&category=math%20test

the Rewrite rule is converting now it as /C123/math%20test.html with 301 flag.

If I convert it thru script, it would become math-test.html but how it would become 301? Should I use 301 redirect using php.

jdMorgan

12:11 am on Oct 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this thread [webmasterworld.com], and read it very carefully and thoroughly. Research anything that isn't clear. That thread describes the entire process you're apparently trying to do here, except for the space-to-hyphen conversion.

URLs *are defined* by the links on your pages. While mod_rewrite can redirect them, the original URL is still the 'real' URL as long as it appears on your pages. mod_rewrite can also be used to 're-connect' a new static URL to the old dynamic server filepath, and to redirect attempted direct access to that filepath by visitors using the old dynamic URL. You need rules to do *all three things*, and you will find examples in that thread.

Jim

fastfriend

8:47 pm on Oct 16, 2008 (gmt 0)

10+ Year Member



what about URL Redirect with 301 using PHP instead mod-rewrite. Which is better ? especially for google indexing and ranking purpose.

g1smd

12:24 am on Oct 17, 2008 (gmt 0)

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



When the browser receives the message "301 Moved Permanently -- new URL is...." it has no idea whether httpd.conf or .htaccess or some PHP script generated that message - and nor does it matter. They all do the same job.

Use .htaccess when it needs to work for almost any URL request that could be thrown at the server, and the pattern matching is quite simple in order to derive the new URL from the old.

Use the PHP method when the URL request has been passed to the script as if it were a valid request, but the data about the new URL needs to be "looked up" in some sort of table or database, rather than just derived by a simple pattern-matching rule of some sort.