3

I have a site that was temporarily set up as 111.222.333.444/~mysite because the real URL was not ready. 111.222.333.444/~mysite/?page_id=123 was a valid page on that site. My site is live now. I no longer want or need to use 111.222.333.444/~mysite to access the site.

However Google has indexed a search for a term on page ?page_id=123 as 111.222.333.444/~mysite/?page_id=123. But ?page_id=123 no longer exists. It's now on a new page, and I want to redirect Google's link to www.example.com/newpage

The closest I have come to implemeting this redirect is:

RewriteCond %{HTTP_HOST}  ^111\222\.333\.444$ [NC]
RewriteCond %{QUERY_STRING}  ^\?page_id=123$ [NC]
RewriteRule ^~mysite/$ //www.example.com/newpage [R=301,NE,NC,L]

But this doesn't work, and gives me an internal server error.

I have also tried:

Redirect 301 /?page_id=195 /newpage

But that had no effect. Can anyone suggest how to do this? I don't have a problem redirecting single pages, but the temporary/alternate URL with the IP address is throwing me.

3
  • Redirecting will keep the old URL alive. I would not do this. I would just let the page 404. Google will eventually remove it from the index and find your correct page. Commented Aug 23, 2016 at 1:53
  • Fair point, but alas I have a client who does not want it to 404! The client is always right :-) Besides, Im puzzled I cant seem to get such an apparently simple redirect working - it isnt hard for the same url, but something is odd about using the ip address. Commented Aug 23, 2016 at 7:38
  • Silly silly client! ;-) You have two of our very best below so I am sure you will be able to redirect the way you want soon. Cheers!! Commented Aug 23, 2016 at 14:54

1 Answer 1

2

The mod_alias documenation states in the top section that it doesn't support redirects based on query strings. You are not going to be able to get a Redirect 301 rule to work.

I see a couple problems with your rewrite rule:

  1. You are missing a period (.) in your ip address rule.
  2. The question mark (?) is not included in the query string so \? should not be part of your QUERY_STRING rule.
  3. mod_rewrite doesn't support protocol relative redirect URLs. Your new URL can't start with //. You need to start it with http:// or https:// unless you do a bunch of fancy hocus pocus.

Also:

  • I don't think you need the no case ([NC]) flag, so I would omit it.
  • I don't think you need the no escape ([NE]) flag, so I would omit it.
  • If your new site doesn't use a page_id=123 query string at all, you could remove the requirement that it has to be on the IP address.
  • Similarly, I would omit the ~mysite part of the rule. Just base the redirect on the the query string, not on the folder it is in.

I would try this rule:

RewriteCond %{QUERY_STRING}  ^page_id=123$
RewriteRule (.*)  http://www.example.com/newpage?  [R=301,L]
2
  • You'll need a ? on the end of the RewriteRule substitution (or use the QSD flag on Apache 2.4+) to remove the query string from the redirected URL. Otherwise, mod_rewrite passes the query string through unaltered, which would result in a redirect loop. +1 Commented Aug 23, 2016 at 11:24
  • Edited in the ? based on w3dk's suggestion Commented Aug 23, 2016 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.