CodeIgniter GET method and SEO friendly URLs - php

In my web page search input I am using the get method, and the URL is something like this www.example.com/search?city=example&service=example2, but I want a URL like this www.example.com/search/example/example2; how can I convert?
Or maybe there is some other solution to get the data from html inputs and get a friendly URL?

I think its help you and also SEO.
Please write following uri rules in : application/config/routes.php
$route['search-(:any)-(:any).html'] = 'search?city=$1&service=$2';
its rewrite your url from www.example.com/search?city=example&service=example2 to www.example.com/search-example-example2.html

In your routes.php file inside the config folder:
$route['search/(:any)'] = 'search/searchFunction'; //You will now need to have a searchFunction() inside the controller search. Or you can just have 'search' and handle the code below inside the index() function.
Now, inside your searchFunction:
function searchFunction()
{
$city = (isset($this->uri->segment(2))) ? $this->uri->segment(2) : null; //if you have www.example.com/search/example
$service = (isset($this->uri->segment(3))) ? $this->uri->segment(3) : null; //if you have www.example.com/search/example/example2
//General Pattern is: www.example.com/uri-segment-1/uri-segment-2/uri-segment-3.....and so on.
// Now when you are querying, if($city) then 'WHERE city = $city' and so on.
//Also, in the link that was earlier triggering 'www.example.com/search?city=example&service=example2'
//like <a href='www.example.com/search?city=example&service=example2'>Link</a>
//Now have it as:
// <a href='www.example.com/search/example/example2'>Link</a>
}

I see others posting about routing, yes i can do like what, but i think first i need to solve problem with html submit action, because when i press the button its auto redirect to what standart format url search?city=example&service=example2, but like i say i need other format. So how i can solve this problem?
My form action
<form method="get" action="http://example.com/search">

Related

Numbered URL in CodeIgniter PHP

I am making a new website and I want the URL to be both in numbered form as well as simple text form (just like stackoverflow.com 's URLS) but I am unable to do so without calling headers as I want that whenever the user visits using url like
http://MY_WEBSITE/654321
then it goes to, say the page is linked to abcd then the URL should be like
http://MY_WEBSITE/654321/abcd
But here both abcd and 654321 are dynamic as they will change across links.
So, how should I do this? How to change to to new URL without header
Any suggestions of implementing in a new way or fixing the problem is heartily welcomed!
This provides you for retrieving information from your URI strings
$this->uri->segment(n); // n=1 for controller, n=2 for method, etc
example URL:
http://test.com/index.php/controller/action/1stsegment/2ndsegment
then it shows!
$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

Codeigniter url redirecting from address url

I have a problem with codeigniter, i can't find a resolv.
How can i make a redirect by parsing into url the destinations url without interpreting the / as a separator betwen controller model etc.
For example:
http://www.example.com/http://www.google.com/?q=test
what it shoud do to thake http://www.google.com/?q=test as a string and put it into
Header("Location: http://www.google.com/?q=test");
any ideeas ?
You can use the url helper's redirect function. If you give it a full address (with http://), it will redirect to that external url.
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
redirect("controller_name/function_name");
this code is using header function in php
it is equivalent tobase_url() + redirect value
I success make that,
I used url library like has sugest Juul, and the code is like (in controller):
class url extends CI_Controller {
function index() {
# the codeigniter not accept the characters after ? and i make a custom code
# to know where is the ? sign and then i replace http:/ whith two //
$url = str_replace(":/","://",str_replace('%5E','?',$this->uri->uri_string));
#the url var will return http://www.google.com/?q=test
}
}

How to generate a new URL using current_url in codeigniter

I am trying to use the current_url() function in codeigniter but not sure how to manipulate this data. I understand I can echo out the current_url() but how do I take each of the segments and use to generate a new URL?
I am trying to take pieces of the current page and use to generate a new URL with adding in new variable data.
I have never worked with current_url(); before, so not sure what to do. Lets say my user is on a filters page and has 2 variables to filter their results (neighborhood and category). If the user clicks on a neighborhood, how would I structure the hyperlink to pull the current url variable for the category and then insert the variable for neighborhood?
My URLs look something like:
mydomain.com/ny/find/$neighborhood/$category
Breakdown:
ny = controller
find = function
param1 = $neighborhood
param2 = $category
Any help would be much appreciated...
You can breakdown your URL by means of this:
For ny: echo $this->uri->segment(1);
For find: echo $this->uri->segment(2);
For param1: echo $this->uri->segment(3);
For param2: echo $this->uri->segment(4);
<?php //check if there is a value in uri segment 3 (param1)
if($this->uri->segment(3)){
//if there is a value there then include it in the link ?>
Link text
<?php //otherwise just go to the link for param1
}else{ ?>
Link text
<?php } ?>
This should work though depending on the exact logic needed you may have to tweak the links and also the logic in the else statements. Depending on how you want to link you may also have to add an elseif($this->uri->segment(4)) and use different logic if the user is on a page with 4 url segments.
current_url() returns a string for the actual page loaded. You can create any url you want using the segments.
Let´s say you have this current url:
mydomain.com/ny/find/$neighborhood/$category
if you want add something to this url and create a link to the user, you can make a string:
$url = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2).'/newParamYouAdd/'.$this->uri->segment(4);
And then:
New url
Let me know if is this what you want.

Adding dynamic id in the url using codeigniter

I have an edit page with url
example.com/product/edit/11
Now if the validation is false I intend to reload the edit page but can't figure how to add that id '11' in the url
Things I have done so far
In routes.php
$route['product/edit/(:num)'] = "product/edit/$1";
Now how do I pass that id in the url from the controller
I tried this
$this->load->view('product/edit/'.$edit_id,$data);
Edit: I am not trying to pass edit_id to the view file
I am trying to simulate this:
Edit
So that the function in my controller
function edit($edit_id)
{
//some code
}
Will have have that $edit_id to work with
It didn't work as I suspected.
I know I can pass that id to the view file and make it work, but I wanted to if I can pass that id in the url.
Thanks
Edit: I figured that even if I managed to pass it in the url. I will end up with more problem like setting the form attributes and other stuff. What I did was
redirect('product/edit/'.$edit_id);
and gave an error message by setting the flashdata.
Your Product.php controller
function edit($edit_id) {
//some codes
//passing edit_id to view
$data['edit_id'] = $edit_id;
$this->load->view('product/edit', $data);
}
And in view/product folder, edit.php view file
//some codes
echo $edit_id
Or directly from url in view file
echo $this->uri->segment(3);

Doing a URL re-write while using PHP GET method

When I click on a comment section for a given entry on a site I have, the URL looks like this:
http://www...com/.../comments/index.php?submission=Portugal%20Crushes%20North%20Korea&submissionid=62&url=nytimes.com/2010/06/22/sports/soccer/22portugalgame.html?hpw&countcomments=3&submittor=johnjohn12&submissiondate=2010-06-21%2019:00:07&dispurl=nytimes.com
I want to make it look like this URL:
http://www...com/.../comments/Portugal-Crushes-North-Korea-62
I understand that this involves adding rules to the .htaccess file. I have two questions:
Since I am using the GET method in PHP, the ugly URL has a bunch of variables appended to it. I don't want all of these variables to appear in the clean URL. Is it possible to only include a few of the variables in the clean URL but still have a rule directing it to an ugly URL with all of the variables?
Once I have the .htaccess rules written, do I go back and change the links in the source code to direct to the clean URLs? If so, how do I do this using the GET method when the clean URL does not have all of the variables that I want to pass along?
Thanks in advance,
John
I'm not sure why you need all that data in the URL. You should be storing things like the submission title, its date and author in a database and then refer to it with an ID. That way, your URLs will be shorter and prettier:
http://www.example.org/article.php?id=1
http://www.example.org/article/1/
You can accomplish this with a simple RewriteRule in your .htaccess file, like so:
RewriteEngine On
RewriteRule ^articles/([0-9]+)/ article.php?id=$1
No, you can not leave variables out and expect them to be passed anyway. If you do this, the information is no longer in the URL, so you don't have a way to get it.
You can use post instead of get if you want to pass variables without them showing up in the URL.
I join the word of Sjoerd, but there are a lot of ways how you can rewrite your url like you want to!
Apache and (IIS too) supports url-s like this one: http://example.com/index.php/my-rewritten-url_62
function URISegment($segment)
{
$uri_array = explode('/',$_SERVER['REQUEST_URI']);
$uri_count = count($uri_array);
$returning_uri = array();
for($i = 0;$i<$uri_count;$i++)
{
if(empty($uri_array[$i]) || $uri_array[$i] == "index.php")
unset($uri_array[$i]);
else
array_push($returning_uri,$uri_array[$i]);
}
if($segment < count($returning_uri))
return $returning_uri[$segment];
else
return false;
}
This works, but you need to define the base url too, and this needs to be called at the beginning of the file, and implemented at every image, script, etc. call.
function BaseURL()
{
if(isset($_SERVER['HTTP_HOST']))
{
$base = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$base .= '://'. $_SERVER['HTTP_HOST'];
$base .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
else
{
$base = 'http://localhost/';
}
return $base;
}
After this you can use instead of this:
// http://example.com/?MyKey=Some-data
$MyKey = $_GET['MyKey']; //which is the first item
echo $MyKey;
// results: Some-data
This:
// http://example.com/?MyKey=Some-data
$MyKey = URISegment(0);
echo $MyKey;
// results: Some-data
You've got the same result by each one.
PS:
I like this solution because I can mix url types as I need them like:
example.com/index.php/index/evaled-article?some=db-stored&code=snipplet
And of course you can rewrite your url like FRKT said :)
And of course, if you want to hide the index.php you need to use mod_rewrite, because there's no way

Categories