Category Page 301 Redirect

Discuss practical ways rearrange URLs using mod_rewrite.

Category Page 301 Redirect

Postby pushpinderbagga » Tue Jul 14, 2009 4:06 pm

hello all... I have an issue here...

I have a page

http://www.example.com/category.php?category=business

and have rewritten it to

http://www.example.com/business

using the below code

Code: Select all
RewriteRule ^([-a-z0-9_]+)/?$ /category.php?category=$1 [NC,L]


now google can see this same page with 2 urls ... so I need to get a 301 redirect to the .php page

which I almost successfully did by using the below code

Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?category=([^&]+)(&.*)?$ [NC]
RewriteRule ^category\.php$ /%2/? [R=301,L]
RewriteRule ^([-a-z0-9_]+)$ /category.php?category=$1 [L]


I dont know if I am correct... but the page results in redirecting it to

http://www.example.com/business/

with a trailing / at the end ?

If I am right at this... is this working correctly ? how can I remove the trailing slash to make it

http://www.example.com/business

thanks in advance
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Wed Jul 15, 2009 12:18 pm

Replace
Code: Select all
/%2/?

with
Code: Select all
/%2?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

category paging 301 redirect

Postby pushpinderbagga » Wed Jul 15, 2009 10:47 pm

hi richardk ... thanks for the code... I appreciate and its working well...
as usual...

could you please tell me how to integrate paging to that ?

below is how its working now...

Code: Select all
RewriteRule ^([-a-z0-9_]+)/page([0-9]+)/?$ /category.php?category=$1&page=$2 [NC,L]


now we have 301 redirected

category.php?category=ABCD to abcd

now we need to redirect 301

category.php?category=ABCD&page=2 to ABCD/page2 etc

any help is as usual appreciated richardk !
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

tried this but now working

Postby pushpinderbagga » Wed Jul 15, 2009 11:06 pm

I tried the below code but it isnt working richardK

Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?category=([^&]+)(&.*)?&page=([0-9]+)?$ [NC]
RewriteRule ^category\.php$ /%2? [R=301]
RewriteRule ^([-a-z0-9_]+)/page([0-9]+)/?$ /category.php?category=$1&page=$2 [NC]
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Thu Jul 16, 2009 1:33 pm

That is more complicated to do. You can only access backreferences/variable from the last RewriteCond, so you must pass them on
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?category=([^&]+)(&.*)?$ [NC]
# Pass on the category name (in %2).
RewriteCond %2/page&%{QUERY_STRING} ^([^&]+/page)&(.*&)?page=([0-9]+)(&.*)?$ [NC,OR]
RewriteCond %2 ^(.+)$ [NC]
RewriteRule ^category\.php$ /%1%3? [R=301,L]

RewriteRule ^([-a-z0-9_]+)(?:/page([0-9]+))?/?$ /category.php?category=$1&page=$2 [NC,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

works for me

Postby pushpinderbagga » Thu Jul 16, 2009 2:22 pm

this works for me if I remove the previous code... thanks ... as usual... perfect solution... :D

I did 1 more thing

there was this url

Code: Select all
http://www.example.com/user.php?user=richardk&page=1


which had to be redirected 301 to

Code: Select all
http://www.example.com/user/richardk/page1


so I tweaked the supplied code for category page as below and made it working ... cheers !

Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?user=([^&]+)(&.*)?$ [NC]
# Pass on the user name (in %2).
RewriteCond %2/page&%{QUERY_STRING} ^([^&]+/page)&(.*&)?page=([0-9]+)(&.*)?$ [NC,OR]
RewriteCond %2 ^(.+)$ [NC]
RewriteRule ^user\.php$ /user/%1%3? [R=301]
RewriteRule ^user/([-a-z0-9_]+)(?:/page([0-9]+))?/?$ /user.php?user=$1&page=$2 [NC,L]


:d

but a similar situation arises in index.php and latest.php files having paging but only 1 paging GET parameter

currently they being redirected as

Code: Select all
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /latest\.php\ HTTP/
RewriteRule ^latest\.php$ http://www.example.com/all/latest [R=301,L]


but what about

Code: Select all
index.php?page=1 being redirected 301 to http://www.example.com/page1


and

Code: Select all
latest.php?page=1 being redirected 301 to http://www.example.com/all/latest/page1


I could not do it because it has only 1 parameter... could you please teach me how to do these...
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Thu Jul 16, 2009 3:38 pm

Like in the original post
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /index\.php\  [NC]
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

RewriteCond %{THE_REQUEST} \ /latest\.php\  [NC]
RewriteRule ^latest\.php$ http://www.example.com/all/latest [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?page=([^&]+)(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ http://www.example.com/page%2? [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?page=([^&]+)(&.*)?$ [NC]
RewriteRule ^latest\.php$ http://www.example.com/all/latest/page%2? [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?category=([^&]+)(&.*)?$ [NC]
RewriteCond %2/page&%{QUERY_STRING} ^([^&]+/page)&(.*&)?page=([0-9]+)(&.*)?$ [NC,OR]
RewriteCond %2 ^(.+)$ [NC]
RewriteRule ^category\.php$ http://www.example.com/%1%3? [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?user=([^&]+)(&.*)?$ [NC]
RewriteCond %2/page&%{QUERY_STRING} ^([^&]+/page)&(.*&)?page=([0-9]+)(&.*)?$ [NC,OR]
RewriteCond %2 ^(.+)$ [NC]
RewriteRule ^user\.php$ http://www.example.com/user/%1%3? [R=301,L]

RewriteRule ^page([0-9]+)/?$ /index.php?page=$1 [NC,QSA,L]
RewriteRule ^all/latest(?:/page([0-9]+))?/?$ /latest.php?page=$1 [NC,QSA,L]
RewriteRule ^([-a-z0-9_]+)(?:/page([0-9]+))?/?$ /category.php?category=$1&page=$2 [NC,QSA,L]
RewriteRule ^user/([-a-z0-9_]+)(?:/page([0-9]+))?/?$ /user.php?user=$1&page=$2 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

works for me

Postby pushpinderbagga » Thu Jul 16, 2009 4:51 pm

thanks richardk ...

works like anything :D

now the only thing remaining is how I target story.php

currently its rewritten as

Code: Select all
RewriteRule ^[-a-z0-9_]+/([0-9]+)/[-a-z0-9_]+\.html$ /story.php?id=$1 [NC,L]


a

Code: Select all
http://www.example.com/story.php?id=12


redirected to something as

Code: Select all
http://www.example.com/CATEGORY/12/TITLE-HERE.HTML


so how can we do to redirect 301 the story.php to the rewritten URL

currently I have disallowed story.php in my robots.txt which should not be done to allow robots to free flow the site...
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Fri Jul 17, 2009 10:39 am

Unless you have access to the httpd.conf file you cannot do that with mod_rewrite because from the number 12, mod_rewrite can't find the category or story title. You will need to redirect with PHP instead.

If you have access to the httpd.conf file, you could use a RewriteMap to give mod_rewrite access to the category and story title.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

I have access

Postby pushpinderbagga » Fri Jul 17, 2009 3:24 pm

I have access to the file... dont know where it is...

I made the site on my own and its my hosting only... so I have all the access needed...

how can we do this ?

the category and story title are saved in the database... and different for every story id...
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron