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

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.

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.

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.

Related

Codeigniter URL routing solution for my website

I have a website that is developed with CodeIgniter. I have added the route for my url as follows:
$route['about_us'] = 'about-us';
Now I have a problem with that. I.e. when I am looking for the url www.mysite.com/about_us it works and at same time www.mysite.com/about-us is also working. I want only one url to work: the one with the underscore.
I have removed this to:
$route['about_us'] = 'about-us';
But the url www.mysite.com/about-us still works. It may cause duplicate content for my website in Google and so more page links also showing. Even I don't have that functions too. Like www.mysite.com/about_us/design. Likewise in about_us controller file index function only there, but design method calling in Google.
How do I resolve this problem?
You actually don't need a route here. The normal purpose of request routing the way you are using it is so that you can use hyphenated URLs when hyphens are not permitted in class and function names. I.E. you want the url to by www.example.com/test-controller, but you can't actually name a controller test-controller because the hyphen is illegal.
If you only want to have the underscored URL such as www.mysite.com/about_us then just remove the route completely and name the controller about_us. With no routing rules the hyphenated url should 404.

External Links in CodeIgniter

I have the following code:
<div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div>
Which takes a users input for a link ($link) and puts the url into an anchor tag. It, however, is not redirecting to the external link but simply amending the base url for the site with the stored URL. I attempted to add 'http://' to the beginning of the submitted link which works unless the user has already supplied http in the link input. Any advice on how to overcome this would be amazing.
Yes, per the documentation, anchor() creates links based on your site's URL.
If things are working as expected when URL's are prefixed with http://, but you're having trouble with users sometimes adding http:// and sometimes not, you could simply check the link to determine whether it's ok, or if you need to prefix it. Here's a basic example using strpos:
if(strpos($link, 'http') === FALSE){
// link needs a prefix...
$link = 'http://' . link;
} else {
// link is ok!
}
...use CodeIgniter's prep_url() function (thanks to #cchana for reminding me of it!):
This function will add http:// in the event that a scheme is missing from a URL. Pass the URL string to the function like this:
$url = "example.com";
$url = prep_url($url);

Rewrite url: serve both query string and block urls

my journey of learning MVC continues and as hard as it is, I'm learning a lot of things I could never learn otherwise. Now I have faced the problem in routing. Currently I'm taking the $_SERVER["REQUEST_URI"] and get the controller and method and any args. When url is in format http://mysite.com/forum/thread/12/1123 there is no problem but I need to catch also requests like http://mysite.com/index.php?forum=12&&thread=1123.
I have read links in threads below but cannot get my head on QSA and I though I would better ask.
Thanks
mod_rewrite: Check for Custom query string in URL?
Rewrite url with query string in htaccess
I ended up writing something like before:
I redirect using htaccess
//No Controller specified in url (The current url is base url like http://example.com/hosanna_framework/)
if(!isset($_GET['base_url'])){
$url = $config["router"]["default_controller"];
}
//Controller is specified in url
else{
$url = $_GET['base_url'];
}

how does URL rewrite work in plain english

I have read a lot about URL rewriting but I still don't get it.
I understand that a URL like
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=19
can be replaced with a friendlier one like
http://www.example.com/Blog/2006/12/19/
and the server code can remain unchanged because there is some filter which transforms the new URL and sends it to the old, but does it replace the URLs in the HTML of the response too?
If the server code remains unchanged then it is possible that in my returned HTML code I have links like:
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=20
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=21
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=22
This defeats the purpose of having the nice URLs if in my page I still have the old ones.
Does URL rewriting (with a filter or something) also replace this content in the HTML?
Put another way... do the rewrite rules apply for the incoming request as well as the HTML content of the response?
Thank you!
The URL rewriter simply takes the incoming URL and if it matches a certain pattern it converts it to a URL that the server understands (assuming your rewrite rules are correct).
It does mean that a specific resource can be accessed multiple ways, but this does not "defeat the point", as the point is to have nice looking URLs, which you still do.
They do not rewrite the outgoing content, only the incoming URL.

htaccess redirection for #! in urls

I am using ajax to load pages on my website.
Each time a page is loaded I change the url in the browser to
http//www.example.com/old/page/#!/new/page/
by setting it through window.loaction using javascript
Now what I want to do is when someone comes to my website by entering the url
http//www.example.com/old/page/#!/new/page/
he should automatically get redirected to
http//www.example.com/new/page/
This is somewhat that happens on facebook too.
Can someone help me out with the required .htaccess code to achieve the same.
Thanks in advance
I don't think anything past the # symbol in your URL is even visible on the server side. So htaccess, php, etc won't even know the hash is there to begin with. I think in order to pull this off you're going to have to use a client side redirect.
window.onload = function(){
// First we use a regex to check for the #! pattern in the hash
if(window.location.hash.match(/\#\!/i)){
// If we found a match, use substring to remove the #! and do a redirect
window.location = window.location.hash.substring(2);
}
};
This example will redirect the user immediately on page load. Unfortunately doing a redirect in this manner won't help the search engines to reindex your site, but thats just one of the pitfalls of using fancy javascript or hash based URL's.

Resources