Clean and friendly URL - php

Similar question asked here: Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter
But I can't get the answer right.
My routes.php
$route[$key] = "home_controller/index";
$route['page/(:num)'] = 'home_controller/index/$1';
My .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
My config.php
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO';
Can't understand the syntax.
Been looking all over in Codeigniter Docs such as; https://codeigniter.com/userguide3/general/routing.html
I just can't seem to find the solution for this.
host/page/1
Is working but not redirecting to the desired page in other words its not corresponding to it's page in the pagination
host/?page=1
Is working but I simply want to get rid of ?page=
I also need to fix a path url for host/category/?page=1
to host/category/page/1

I check your code,
As I notice your routing was fine. But your mistake in the config.php file.
Please, replace your code from
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '',
$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO';
to
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
$uri = 'https://';
} else {
$uri = 'http://';
}
$uri .= $_SERVER['HTTP_HOST'];
$config['base_url'] = $uri;
$config['base_url'] .=
preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

Related

CodeIgniter: CSS styles broken on IP address vs domain name

I have this working base_url setting in my
Config.php:
$protocol = is_https() ? "https://" : "http://";
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "";
if(is_cli())
{
$config['base_url'] = '';
}
else if( stristr($host, "localhost") !== FALSE || (stristr($host, '192.168.') !== FALSE) || (stristr($host, '127.0.0') !== FALSE) )
{ // if local
$config['base_url'] = $protocol.$host."/project2/";
}
else
{ // if server
$allowed_hosts = ['website.com', 'www.website.com'];
$config['base_url'] = in_array($host, $allowed_hosts) ? $protocol.$host."/project2/" : "unknown-host.com";
}
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI'; // changing to AUTO didn't work too
// possible base_url results are http://www.website.com/project2 and http://192.xxx.xxx.xxx/project2
My project2/.htaccess code:
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
When I run the site at www.website.com/project2 - everything is fine, but when I run at 192.xxx.xxx.xxx/project2 - css styles are broken
My css links are like this:
<link rel="stylesheet" href="<?php echo base_url();?>assets/stylesheets/somestyle.css" />
How can I fix this?
Silly me! I just changed permissions (to grant all) on my .css files and all worked well now!
My CSS links we're blocked by a file permission that's why my links are broken.
Thanks you guys for all the hints specially on the view-source part. Really helped me a lot!

404 error in codeigniter pagination

I see a lot of this question, but I couldn't solve the issue.
I suspecting that it doesn't read the right segment as I have a reroute config and htaccess to remove the index page.
The code is throwing a 404 page.
Here is my Model:
public function get_city_reviews($city_id,$limit,$offset) {
$list = array();
$this->db->from('biz');
$this->db->where('city_id',$city_id);
$this->db->order_by('created_at','desc');
$this->db->limit($limit,$offset);
$query = $this->db->get();
foreach($query->result() as $row) {
$list[] = $row;
}
return $list;
}
My Controller:
$slug = $this->uri->segment(1,'');
if($slug)
{
$this->load->model('catsAndCities','cc');
$this->cc->set_table_name('city');
$city = $this->cc->get($slug,'slug');
if(!$city || $city->parent_id != 0)
{
show_404();
}
else
{
$this->tank_auth->set_user_city($city);
}
}
else
{
$city = $this->tank_auth->get_user_city();
}
$this->load->library('pagination');
$config = array();
$base_url ="local/index/";
$config["base_url"] = site_url().$base_url;
$config["total_rows"] = $this->bizs->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 4;
$config['full_tag_open'] = '<p class="pageBar">';
$config['full_tag_close'] = '</p>';
$config['num_links'] =2;
$config['anchor_class'] = 'class="page-num" ';
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->bizs->get_city_reviews($city->id,$config["per_page"],
$page);
$data["pagination_links"] = $this->pagination->create_links();
$data['reviews'] = $data["results"];
$this->load->view('local/index',$data);
My View:
<div id="pagination-container">
<div class="pagination">
<ul>
<?=$pagination_links?>
</ul>
</div>
This is my Routes:
$route['default_controller'] = "local/city";
$route['[0-9a-zA-Z\-_]+'] = "local/city";
$route['scaffolding_trigger'] = "";
This is my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
As i suspected, you are telling CI to find 404 page if no city is found. Try commenting this : Or find a way to work around it.
if(!$city || $city->parent_id != 0)
{
show_404();
}
else
{
$this->tank_auth->set_user_city($city);
}
Then change
$base_url ="local/index/";
to
$base_url ="";
And your segment to 1
$config["uri_segment"] = 1;
That should do it.
Try changing the base_url to "local/city", and the uri_segment changed to 3.

htaccess rewrite or php url exploding

I am trying/attempting to create my own MVC. I am really trying to manipulate the url. I am trying to get several parameters to form a full URL.
I want to turn
http://example.com/?action=account&user=JohnDoe
to
http://example.com/account/JohnDoe
But I cant seem to get the .htaccess file to work right :/
This is what I have
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-=_?]+)/?$ index.php?action=$1&user=$2 [NC,L]
When I go to http://example.com/account/JohnDoe I get a 404 error.
Your missing one parameter, try this
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?action=$1&user=$2 [NC,L]
Rather than set up a mod_rewrite rule for that specific URL, why not put an entire framework in place?
mod_rewrite as follows:
#URI PATH CONSTRUCTION
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?string=$1 [L,QSA]
</IfModule>
Then have your index.php file break up $_REQUEST['string'] into an array that you can test to direct the request to where it needs to go
if (isset($_GET['string'])) {
//DEALS WITH GET PARAMETERS
if (strstr($_GET['string'],"?") !== false) {
$pos = 0;
$tailstr = substr($_GET['string'],$pos);
$endpos = strpos($tailstr, "?");
$endpos = $endpos + strlen("?");
$string = substr($_GET['string'],$pos,$endpos);
} else {
$string = $_GET['string'];
}
$path = explode("/",$_GET['string']);
}
//Depth to 5 levels
for ($i=0;$i<5;$i++) {
if (!isset($path[$i])) $path[$i] = null;
}
global $path;
You now have a global array containing the individual elements in the URI
eg
http://example.com/account/JohnDoe
would give you:
$path[0] = 'account'
$path[1] = 'JohnDoe'
$path[2] = ''
$path[3] = ''
$path[4] = ''

product detail page routing in codeigniter

In my local site
http://localhost/giftsware/nl/products/details/2382
This is my browser url for product description page
now I want more user friendly url for product description page. I want to change above url to
http://localhost/giftsware/products/2382
I tried by routing all calls to details by adding
$route['products/(:any)'] = "nl/products/details/$1";
this is in routes.php file, but it gives me 404 error.
What could be the possible issue and how can I fix it?
this is my complete route file code
$route['(:any)/products/(:num)'] = "products/details/$2";
$route['default_controller'] = "pages";
$route['404_override'] = '';
$route['^en/admin/([a-zA-Z_-]+)/(:any)'] = '$1/admin/$2';
$route['^en/admin/(login|logout)'] = 'admin/$1';
$route['^en/admin/([a-zA-Z_-]+)'] = '$1/admin/index';
$route['^nl/admin/([a-zA-Z_-]+)/(:any)'] = '$1/admin/$2';
$route['^nl/admin/(login|logout)'] = 'admin/$1';
$route['^nl/admin/([a-zA-Z_-]+)'] = '$1/admin/index';
$route['admin'] = 'admin';
$route['pages/(:any)'] = "pages/index/$1";
$route['^nl/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
$route['^nl$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
Try it
$route['^nl/products/details/(\d+)$'] = 'products/$1';
Using .htaccess files
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^products/(\d+)*$ ./nl/products/details/$1
Using php
<?php
#remove the directory path we don't want
$request = str_replace("nl/products/details", "products", $_SERVER['REQUEST_URI']);
?>

url rewriting and passing variable

Hi i'm trying to send data over from a url rewrite page but unfortunately, its not working its just throwing me back to the main page or error page. The template & urlrewrite for rmbitter is what i'm trying to do. Currently domain.com/rmbitter.php loads perfectly file. I have a link on domain/boothwall.html and that link looks like this domain.com/rmbitter.php?ulnk=$usr&slnk=$lnk the rmbitter.tpl has the $_GET code. My problem I believe is with the rewrite its not allowing the variable to be passed over.
The reason for using the .tpl is because the page has a design layout that is needed. If i build a started rmbitter.php file with a test.html page and a link to rmbitter.php with the variable it works fine.
$inc = array(
'pictures' => 'icons.php',
'view_images' => 'templates/view_images.tpl',
'boothw' => 'templates/boothw.tpl',
'rmbitter' => 'templates/rmbitter.tpl'
);
//URL rewriting rules...
$rew = array(
'/view_images_public\/(.*)$/' => 'req=view_images&user=$1',
'/boothwall\.html$/' => 'req=boothw',
'/rmbitter\.php$/' => 'req=rmbitter'
);
url_rewrite.php
<?php
//get request
$url = $_SERVER['REQUEST_URI'];
if (strpos($url,'?PHPSESSID=')) $url = substr($url,0,strpos($url,'?PHPSESSID='));
while (strpos($url,'//') !== false) $url = str_replace('//','/',$url);
$url = substr($url,strlen(constant('dir')));
$url_array = explode('/', $url);
//make request string
$reqstr = '';
foreach ($url_array as $key => $value)
$reqstr .= '/'.$value;
$reqstr = substr($reqstr,1);
//other stuff
if (substr($reqstr,0,9) != 'index.php') {
$rewrite['/pages\/(.*)\.html$/'] = 'req=pages&id=$1';
$rewrite['/static\/(.*)\.html$/'] = 'req=static&id=$1';
$rewrite['/(.*)\.html$/'] = 'req=$1';
?>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]
</IfModule>
I don't 100% understand what your problem is, but the general cure to all GET related mod_rewrite ailments is Query String Append (QSA).
RewriteRule ^(.*) index.php [L, QSA]
as the name says, it appends any incoming GET data to the new URL and passes it on to index.php.

Categories