3

I have submitted my site with ugly URLs to Google, and now they are appearing as mysite.com/show.php?id=42.

Now, I know that I can 301-redirect it to mysite.com/article/42. However, my site uses the URL form like stack exchange, so like this mysite.com/article/42/title-of-my-article which is in fact index.php?what=content&id=42&title=title-of-my-article.

Therefore, I cannot use only .htaccess for this, right?

So will it work if I do this?

  • Keep show.php as the redirect node
  • if user comes to show.php?id=42 , get the article of title with id=42, and echo "<meta http-equiv='refresh' content='0; url=http://mysite.com/article/42/$title'>

It will work for the visitors, but will it help Google to understand and change the URL to the new one?

  • I don't see why you can't do these redirects in htaccess? – Max Jul 15 '13 at 10:06
  • because .htaccess can only change the position of the value submitted in the URL. I also want to add the title of the article with that id. – jeff Jul 15 '13 at 17:40
2

In your show.php it looks like you have the title you need for the URL. So instead of echoing the meta tag, use PHP to do a 301 redirect:

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://mysite.com/article/42/$title"); 
  • Wow ! Thanks. I didn't know php can 301-redirect too :) But I heard that header(); function is deprecated or something. Do you think this is valid ? – jeff Jul 15 '13 at 17:41
  • No it's not deprecated at all. Here's the official manual page: php.net/manual/en/function.header.php – DisgruntledGoat Jul 15 '13 at 18:12
  • Oh, now I remembered why I was afraid of header(); you should NOT echo anything before the header(); call. Or else, you will get the error "cannot modify headers, header already sent. output started at line:xx"; – jeff Jul 16 '13 at 14:29
  • Yes that is true. However, you should generally not be echoing content until everything is ready anyway (you should do all your processing first, then have some kind of template or include where you output the HTML). – DisgruntledGoat Jul 16 '13 at 14:49
  • yes, of course. I was just testing if I was echoing the true link to redirect :) – jeff Jul 16 '13 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.