sanjay kushwah
sanjay kushwah

Reputation: 437

Which redirection to use when redirecting URL to SEO Friendly URL?

I am developing a website using Codeigniter.

i want to redirect a URL to its SEO Friendly version. for eg.
I have URL

A. http://www.example.com/post/[post-id]/

I want this URL to redirct to SEO Friendly version of itself

B. http://www.example.com/post/[post-id]/[post-title]

Just like stackoverflow is using and like redirecting A URL to B URL.

http://stackoverflow.com/questions/[question-id]/[question-title]

I was using 302 redirection in the Codeigniter until i read somewhere that if you are using 302 redirection.
then google might treat you as a Spammer
but then again when i saw Stackoverflow URL Pattern then i think its much better to have B version of URL.

So my Questions are:
1. Which redirection stackoverflow is using?
2. Is it better to Store the Slug for [post-title] in database or manually calculate it with url_title() function.

Upvotes: 2

Views: 1036

Answers (2)

Tony McCreath
Tony McCreath

Reputation: 3409

302 means a temporary redirect. The result is search engines will still index the original URL.

301 mean permanent redirect. This results in the search engines transferring index data to the new URL.

302s are not spam but if used in the wrong situation you don't helping yourself.

In your case you will be permanently moving your URLs so a 301 is appropriate.

Upvotes: 2

Abdul Haseeb
Abdul Haseeb

Reputation: 558

create a private function in the posts controller for SEO friendly URLs for posts.

private function _redirect($url) {    // $url = http://www.example.com/post/[post-id]/
  redirect($url . $this->post_title); // becomes http://www.example.com/post/[post-id]/[post-title]
  return;
}

You should have a post_title member variable set before redirection. Where ever you are calling a redirect() function, replace it with your private function _redirect() and you are good to go.

by default its 302 redirect. To do a 301 redirect just add parameter to the redirect() function. e.g

// with 301 redirect
redirect($url . $this->post_title, 'location', 301);

Note: In order for this function to work it must be used before anything is outputted to the browser since it utilizes server headers.

Upvotes: 0

Related Questions