CodeIgniter with less URL segments - php

As you might know, the URL for codeigniter is build like this. In my case, I have:
http://example.com/subfolder/class/function/ID
Is there a way with codeigniter (or htaccess) to create this url in a much shorter url like this:
http://example.com/ID
Kind regards, Senne

In the application/configs/routes.php file, you should be able to add something like this:
$routes['(:num)'] = 'your_controller/your_function/$1'
You can read more about custom routing here: http://codeigniter.com/user_guide/general/routing.html

You can do this by using Url Routing in CodeIgniter.
$route[':any'] = "subfolder/class/function/$1";

Related

Codeigniter URL ReWriting Issue

I am working a codeigniter project. for example I have a controller Blog and a method category with param.
When i go to a link
Developer Blog
it works fine. which redirect to a page developer blog.
Is it possible to rewrite the url as domain.com/blog/developer-blog dynamically ? Thank you.
Open your application/config/routes.php and try following (if you want exact as mentioned in question)
$route['blog/developer-blog'] = 'blog/category/2';
$route['blog/user-blog'] = 'blog/category/3';
for dynamic routing you may use this
$route['blog/developer-blog/(:num)'] = 'blog/category/$1';
$route['blog/user-blog/(:num)'] = 'blog/category/$1';
so your URL now should be looks like https://domain.com/blog/developer-blog/1, https://domain.com/blog/developer-blog/2
or https://domain.com/blog/user-blog/1, https://domain.com/blog/user-blog/2 etc
for more see http://www.codeigniter.com/user_guide/general/routing.html

How to remove a word from the url in codeigniter without using .htaccess

I have a site with urls:
http://example.com/controller-name/page/about
http://example.com/controller-name/page/contactus
http://example.com/controller-name/page/services
But I need the urls like:
http://example.com/controller-name/about
http://example.com/controller-name/contactus
http://example.com/controller-name/services
How to do this in Codeigniter?
You can configure URI Routing to achieve this. Refer Codeigniter documentation for a detailed explanation.
You can map a URL string to its corresponding controller class/method
Hope this one help you.
$route['controller-name/(:any)'] = "controller-name/your method";

URL rewriting using Codeigniter for specific rule

On my local server, I have setup a website using Codeigniter which opens up fine using below URL
I have a different kind of query regarding rewriting URL
http://localhost/mywebsite
Actually the real URL is below
http://localhost/mywebsite/page/
Which I want it to work as root url as
http://localhost/mywebsite
So I have done it by adding it as default controller in config.php
But now I have another URL as
http://localhost/mywebsite/page/mypagename
Which should open as
http://localhost/mywebsite/mypagename
How can I do it?
You can define a custom route in /system/application/config/routes.php - for example:
$route['abc'] = 'controller_name/abc';
Then, http://mydemosite.com/abc
goes to http://mydemosite.com/controller_name/abc
see https://ellislab.com/codeigniter/user-guide/general/routing.html for more information of URI Routing.
I hope this can be helping you.
I got it solved using routing method as below under config/routes.php
$route['([^/]+)/?'] = 'page/$1';

Is it possible to get URL slash/ parameters with simple php?

is it possible to get parameters like:
index.php/bar/foo with php?
I know that I can use s.th. like index.php?a=bar&b=foo and the user $_GET['a']. But I need to do it with the other way.
The answers here just look like guesses. mod_rewrite presumes apache webserver (we don't know which one you use) and is way too much for this simple task.
Apache's default behaviour is to map index.php/xx/xx to index.php?xx/xx.
Look how easy this is:
$args = explode('/', $_SERVER['QUERY_STRING']);
print_r($args);
EDIT: As DanFromGermany pointed out it is actually possible to have urls like index.php/bar/foo without using mod_rewrite or such. I guess the key here is to have a filename (index.php) in the url.
This has to be done in the web server.
In apache there is a module called mod_rewrite which can rewrite urls like index.php/bar/foo to another format such as index.php?a=bar&b=foo.
Examples, and a description of the topic: http://www.seochat.com/c/a/search-engine-optimization-help/creating-search-engine-friendly-urls-with-php/
What web server are you using?
I used following code to access slash parameters:
$args = $_SERVER["REQUEST_URI"];
$arg_arr = explode("/",$args);
print_r($arg_arr);
Hope this helps someone
To achieve the same either you work on httpd.conf for rewriting the URL or use PHP framework like codeigniter etc. which provide inbuilt functionality.

using different url for a file in codeigniter

Hello I want to change the URL of a page
at the moment it looks like
www.domain.com/privacy
and I want it to look like
www.domain.com/privacy-policy
since it looks much better. Is there a way to achieve this in codeigniter?
CodeIgniter uses the routes defined in config/routes.php to rewrite URLs.
Add a line like this: $routes['privacy-policy'] = 'controller/action';
For full documentation, read the user guide:
http://ellislab.com/codeigniter/user-guide/general/routing.html

Categories