Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Cengiz Frostclaw Jul 15 '13 at 17:40
add comment

1 Answer

up vote 2 down vote accepted

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"); 
share|improve this answer
    
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 ? –  Cengiz Frostclaw 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"; –  Cengiz Frostclaw 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 :) –  Cengiz Frostclaw Jul 16 '13 at 21:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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