Page Rank friendly redirect

Hello,

I’m trying (for almost all day now) to write rewritecond and rewriterule lines to redirect old indexed and ranked pages to the new friendly permalinks (wordpress site). But I’m doing something wrong; been googling but can’t find the right answer.

What I’m trying to do:
redirect (with 301) from www.domain.com/info/?page_id=1 to www.domain.com/tools/

The content of .htaccess

# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

RewriteEngine On

# PAGERANK redirects
RewriteCond %{QUERY_STRING} 'page_id=1' [NC]
RewriteRule . http://www.domain.com/tools/ [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

What am I doing wrong? I can’t find the sollution.

Hi, and welcome to SitePoint :wave:


# PAGERANK redirects
RewriteCond %{QUERY_STRING} 'page_id=1' [NC]
RewriteRule . http://www.domain.com/tools/ [R=301,L]

There are 2 things incorrect here:

  1. You shouldn’t quote page_id=1
  2. You don’t want redirect everything to /tools/ do you, just the requests for /info, right? You need to reflect that in the rule.

So what you’d get is


RewriteCond %{QUERY_STRING} page_id=1 [NC]
RewriteRule ^info/?$ http://www.domain.com/tools/ [R=301,L]

( assuming Apache 2.x. If it’s 1.x change ^ to ^/ )

A few comments on the rest of the code if I may:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

  1. Find out once if mod_rewrite works on your server, and if it does remove <IfModule mod_rewrite.c> and </IfModule>
    Right now you’re asking Apache for every request is mod_rewrite is enabled, which not necessary if you know it is.

  2. You don’t have any Redirect* directive in your .htaccess, so you can get rid of the RewriteBase /

  3. Change /index.php to just index.php, i.e., remove the leading slash. With the leading slash Apache will first look in the root of the filesystem to see if there is an index.php there. Only when it finds there isn’t one will it look for index.php in your document root.

Okay Great, making progress here!

It is working, but I have in the URL of my browser:

www.domain.com/tools/?page_id=1 (after the rewriterule)

(so it is sticking the original Query_string to it).
Can we get rid of the query string? I don’t want google to think there are 2 different pages.

I had already read your comment on the “ifmodule” lines, but I wanted it to work before changing things in 2 places and wondering what might cause what problem :slight_smile:

You can eliminate the query string by appending a ? to the new URL:


# PAGERANK redirects
RewriteCond %{QUERY_STRING} 'page_id=1' [NC]
RewriteRule . http://www.domain.com/tools/[COLOR="Blue"][B]?[/B][/COLOR] [R=301,L]

The ? itself will not be part of the new URL, but will prevent Apache from copying the query string.

:slight_smile:

Okay, I think I got it; it’s working. Great. :slight_smile:

But now in this case I need to do a rewrite form all “.” instead of “^info/?$”

RewriteRule . http://www.domain.com/tools/? [R=301,L]

Can you explain that to me? We’ve got a match in the preceeding rewritecond
and it was working but got the extra query. But why do we need the “.” now instead of the “^info/?”.

Is there a place where I could have found more of these extras (the appening “?”) explained?

Thanx!

You really shouldn’t be needing that dot. That way it will redirect everything that has page_id=1 in the query string to /tools. So not just /info/?page_id=1 , but also /test/?page_id=1 , /bla/?page_id=1 , etc
Which is why I suggested ^info/?$
If that doesn’t work you may be on Apache 1.x, in which case you’d need to make it ^/info/?$.

Good sources for Apache mod_rewrite are this article and of course the [URL=“http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html”]Apache manual on mod_rewrite.

Yes, I understand, but the weird thing is that as soon as I change

RewriteRule . http://www.domain.com/tools/? [R=301,L]

in

RewriteRule ^/info/?$ http://www.domain.com/tools/? [R=301,L]

the rewrite isn’t being performed. (Also tried "^
It did when I didn’t have the “?” added on the rewritelocation…

My apache version = 2.2.17 …

Oeps … Found it; I had the “/” before (like in the quote above…).

Thanks for the help!

Working, solved.

Good, I’m glad :slight_smile:
If you have any more questions feel free to come back any time :slight_smile: