CodeIgniter Forums
URL rewriting problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: URL rewriting problem (/thread-65642.html)



URL rewriting problem - xenos92 - 07-05-2016

I'm locked with my system... I changed the settings in my config.php file to have URLs with parameters. Suddenly I have a product controller that displays the id that I pass URL parameter.

http://localhost:8888/mywebsite/index.php?c=product&m=index&id_product=12

This URL works now I want to get a url of the type:

http://localhost:8888/mywebsite/product/my-product-12

So I put this in the .htaccess file:



Code:
RewriteRule ^product/([a-zA-Z0-9\-]+)-([0-9]+).html$ index.php?c=product&m=index&id_product=$2 [L]

But the page displays : The requested URL /index.php/product/my-product-12.html was not found on this server.

My htaccess file :


Code:
#Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1         [L,QSA]

RewriteRule ^product/([a-zA-Z0-9\-]+)-([0-9]+).html$ index.php?c=product&m=index&id_product=$2 [L]
My config.php file :

My config.php file:


Code:
<?php

$config['base_url'] = 'http://localhost:8888/mywebsite/';
$config['index_page'] = '';
$config['uri_protocol'] = 'QUERY_STRING';
$config['url_suffix'] = '';
$config['enable_hooks'] = TRUE;
$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['directory_trigger']    = 'd';
$config['rewrite_short_tags'] = FALSE;

?>



RE: URL rewriting problem - RogerMore - 07-08-2016

Hi Xenos92,

I don't know about your approach, but if I wanted to do something like http://localhost:8888/mywebsite/product/12, I would probably do something like this:

.htaccess:
First of all set change the rewritebase in your .htaccess file to /mywebsite/.

Code:
RewriteBase /mywebsite/


Then delete the new rewrite rule from the .htaccess file.

Code:
#RewriteRule ^product/([a-zA-Z0-9\-]+)-([0-9]+).html$ index.php?c=product&m=index&id_product=$2 [L]


config.php:

PHP Code:
$config['enable_query_strings'] = FALSE


routes.php:

PHP Code:
$route['product/(:num)'] = 'product/product_lookup_by_id/$1';

$route['default_controller'] = "aanmelden";
$route['404_override'] = ''


So with setup the url http://localhost:8888/mywebsite/product/5 is routed to the product_lookup_by_id method from the product controller.
In the method you can use $this->uri->segment(3) to catch the id.

If you really want to do http://localhost:8888/mywebsite/product/my-product-12, change :num in the new route in :any, and figure out a way to get the id from the segment you retrieve.

Hope this helps...

- Roger