Using '?' in route for old URLs - php

We have a bunch of URLs like this /order/step1.php?service=999 out there on the Internet.
Do you have to just abandon all of your old links that are published out on the Internet when you migrate to Laravel?
We tried to use mod_rewrite to simply rewrite the URL to /order/step1/999 but because of the .htaccess for Laravel that isn't working.
RewriteRule ^/order/step1\.php\?service=([0-9]+)$ /order/step1/$1 [L,QSA]
We also tried adding a route into web.php like this:
Route::get('/step-1.php?service={slug}', 'PageController#step1');
and that doesn't work either.
We would like to be able to not lose all of our old URLs.

Given that the url is like this:
/order/step1.php?service=999
The ?service=999 segment can be taken as a query param. So I think this should do the trick:
Route::get('order/step-1.php', 'PageController#step1');
Then in your controller:
# PageController.php
public function step1(Request $request)
{
$slug = $request->query('service');
dd($slug) // '999'
}
Check the section Retrieving Input > Retrieving Input From The Query String of the documentation.

Related

How to rewrite an URL to a specific controller class with htaccess file in codeigniter

I have 2 controller classes in my application named Localhost/electronics.
The URL "Localhost/electronics/cameras" goes to
"localhost/electronics/home/details/cameras".
This is happening because of the rule
$route['(:any)'] = "home/details/$1";
2nd controller class specifics() and it's method showspecifics() is accessed through URL
`"localhost/electronics/specifics/showspecifics/camera1.`
How can I do the following?
Only with the help .HTaccess file, I want to.. be able to access the second class specifics() using URL
`"localhost/electronics/camera1` .
I am aware that using
`$route['(controllername/:any)'] = "specifics/showspecifics/$1";`
is a possible way close to achieving the clean URL but it's not what I want.
Please advise as to how to use htaccess to accomplish this.
Any idea is greatly appreciated.
Usually, you would capture the final part of the URL and then rewrite the request, e.g.
RewriteCond %{REQUEST_URI} !^/electronics/specifics/showspecifics/
RewriteRule ^electronics/(.+)$ /electronics/specifics/showspecifics/$1 [L]
The RewriteCond is there to prevent a rewrite loop.
This doesn't work with CodeIgniter however, because CodeIgniter looks at REQUEST_URI to determine the controller and method to serve this request. But REQUEST_URI isn't changed by the RewriteRule and remains /electronics/camera1, and CodeIgniter doesn't find an appropriate controller.
To change REQUEST_URI, you had to redirect instead of rewrite, e.g.
RewriteRule ^electronics/(.+)$ /electronics/specifics/showspecifics/$1 [R,L]
but this also changes the client's URL bar, which isn't desired in this case.
So, there's no way to achieve this with .htaccess and CodeIgniter.
To do this in CodeIgniter, you would use appropriate routes in application/config/routes.php like
$route['(cameras|smartphones|computers)'] = 'home/details/$1';
$route['(:any)'] = 'specifics/showspecifics/$1';
This handles the few categories by controller and method home/details, and everything else by the controller and method specifics/showspecifics.

Codeigniter regexp routing

I have a problem with routing like this:
$route['(/[a-z]{2}/)'] = 'locale/somepage';
And in .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(/[a-z]{2}/)$ /index.php/locale/somepage
I need replace first section of url (class or controller) and call an another controller. For example, I need to url as /en/page will call controller locale, but url need not be changed.
This code is not working. And if I try use only routes.php or only .htaccess, it not working too.
How I can make it work?
I think you've written your htaccess regexp rule wrong. You don't need to write it as a PHP regexp rule, try to rewrite it without the slash in your htaccess:
RewriteRule ^([a-z]{2})/page$ /index.php/locale/somepage
This will send any http://mypage.com/en/page to index.php/locale/somepage.
With that rule, CI will receive the url index.php/locale/somepage. At that point, CI will go to routes.php and will check if there are any rule to call a specific controller. If not, it'll try to go to a controller called locale, to load a method called somepage.
So, you don't need to use routes.php to modify again the apache url that you're receving to call another controller.

custom URL mappings in codeigniter like redmine?

So redmine has a very peculiar url mapping style that i observed :
http://demo.redmine.org/projects/<project-name>/controller/action
samples :
http://demo.redmine.org/projects/produto/activity
http://demo.redmine.org/projects/produto/issues/new
http://demo.redmine.org/projects/produto/issues/gantt
http://demo.redmine.org/projects/produto/files
and the url changes as the project changes.
how do i do this in codeigniter ? I'm thinking it can be done with routes.php but so far i'm not able to get anywhere.
Looking for any help. Thanks.
Use the following function inside your "application/controllers/projects.php" controller:
public function _remap($method)
{
if ($method == 'project-name')
{
//display project1
}
elseif($method == 'project-name2')
{
//display project2
}
}
You can do the same for varying methods by extracting them from database
take a look here:
http://codeigniter.com/user_guide/general/controllers.html#remapping
you can also route your controller by using custom routes in application/config/routes.php
$route['example'] = "controller/function";
$route['example2/(:any)'] = "controller/function";
You use the routes file in application/config/routes.php
You would use something like this:
// the $1 maps to :any
$route['projects/produto/:any'] = "$1";
// the $1 maps to the first any, $2 maps to the second :any
$route['projects/produto/:any/:any'] = "$1/$2";
You will want mod_rewrite enabled if you are handling clean URL's. Otherwise expect the index.php/controller/action. I cant test it myself there, but you should refer to:
Once you add a route (It has to be called $route[] inside the configuration), refresh the page and try to go to the URL!
http://codeigniter.com/user_guide/general/routing.html
add this to your routes.php (btw: you need url-rewriting enabled for routes to work, ie. using .htaccess)
$route['projects/(:any)/(:any)/(:any)'] = "$2/$3/$1";
for example /projects/produto/issues/new will call the function new in the class issues and pass it the parameter 'produto'
also check http://codeigniter.com/user_guide/general/routing.html

Hiding default controller name in PHP CodeIgniter URLs

I am trying to remove the default controller name from URLs in CodeIgniter using htaccess; I have hidden index.php but also want rid of the default controller which is currently called con_index.
For example if site root was mysite.com,
mysite.com/con_index/function1 would change to mysite.com/function1 and so on.
All other controllers can remain in the url, so if I had another controller called locations with a function called location1, mysite.com/locations/location1 would stay the same.
I think this makes for a more conventional structure rather than a class and function name popping in there the second you leave the site root. Gnashed my teeth trying to achieve this, can anyone help?
Try this inside your .htaccess :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\/$ index.php/com_index/$1 [nc] [L]
This would replace any occurrences inside brackets with $1. So, when you're calling www.example.com/function_1/ it actually calls www.example.com/com_index/function_1
However, I'm not sure it works for CI because CI might have some restrictions for accessing the URL Route.
Have you tried using
$route['default_controller'] = 'con_index'
CodeIgniter Default Controller
enter code here$route['(:any)'] = 'controller_name/function_name/$1';
it will replace controller and then u can try to access url by not putting controller name in it.

CodeIgniter htaccess Redirect

I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.
Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?
Usually in htaccess I could write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.
Any suggestions?
CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:
http://localhost/user/info/gregavola
Then in the user controller, add the following stub:
function info($name)
{
echo $name;
}
From here you would probably want to create a view and pass $name into it:
$data['name'] = 'Your title';
$this->load->view('user_info', $data);
You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.
To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:
$route['(:any)'] = "user/info/$1"
Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.
Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html
Good luck!

Categories