Forum Moderators: phranque
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]
[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.
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]
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]
# 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]
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]
# 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]
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
&?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".
# 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]
Jim
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.
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
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.