Yii2 how to rewrite url without controller name - url-rewriting

I am rewriting my yii2 website urls.In my config file i added
'<category_name>-<controller>-<category_id>'=>'<controller>/index'
and in the url i just passed the parameters like
<?=Url::to(['shop/index','category_id'=>1,'category_name'=>'clothes'])?>
And my url comes like
https://example.com/clothes-shop-1
This is what i am getting. But i need something like
https://example.com/clothes-1
for that i just changed the rule like this
'<category_name>-<category_id>'=>'<controller>/index'
But that time rewriting doesn't working.How can i remove the controller name from that url

How does system know, what controller is needed to proceed URL? In first example
'<category_name>-<controller>-<category_id>'=>'<controller>/index'
There is a controller name. In second
'<category_name>-<category_id>'=>'<controller>/index'
No controller name.
So you need to tell it. Try
'<category_name>-<category_id>'=>'shop/index'

Related

How to load an codeigniter application by using different url

Now I am developing an application by using CodeIgniter framework. In this application, there is a section which name account setup. By using this section we can create multiple accounts. As an example, accounts name are abc, bca or anything. And Suppose my site URL is: www.xyz.com (base_url) Then we need to access the site by the domain name and also domain name with account name like bellow:
www.xyz.com
www.xyz.com/abc/
www.xyz.com/bca/
www.xyz.com/anything/
For More clear, URL pattern will be:
www.xyz.com/dashboard/index
www.xyz.com/abc/dashboard/index
www.xyz.com/bca/dashboard/index
How can we do it? I need your suggestion.
Yes it's Possible through codeigniter Routs.
Go to application\config Open a file which Name is routes.php
you can call the controller functions see in code example
example
$route['new_project'] = "controller/function";
when you hit your website url like this www.xyz.com/new_project it will go to your mention controller and function.
see the documentation of routs CodeIgniter URI Routing
example 2:
$route['account/(.*)'] = "controller/function";
Now we you redirect your url like this
redirect('account/'.$name_var.'');
Now your url look like this.
www.xyz.com/account/name_of_logged_in_user
Also you can use this like this.
$route['(.*)/dashboard/index'] = "controller/function";
And then you can call it like this.
redirect(''.$name_var.'/dashboard/index');
And from this code your output url show something like this.
www.xyz.com/name_of_logged_in_user/dashboard/index
Hope it will help you if any question add comment
There are many way to do it.
1) Domain alias if you want to run same script with different domain.
2) if you want to run same script with sub domain then you can do it from Cpanel
3) If you want to run same script with different folders in same domain then you need to use htaccess

Encrypt URL with PHP Codeigniter

I have developed a website with PHP Codeigniter. It consists of many controllers and views. I want to encrypt the URL of the site to hide the controller and function name. Currently, the URL looks like this :
www.sitename.com/controller_name/function_name
I don't want the users to see the controller and function names. Is it possible to do this now because the site is almost complete. Is there any shortcut of doing this? Please Help
You can use codeigniter URI Routing to remove your controller name from URL.
$route['url_name'] = 'controller_name/function_name';
This `www.sitename.com/url_name` will look go to `www.sitename.com/controller_name/function_name`
$route['url_name/(:any)'] = 'controller_name/$1';
This will only change your controller name.. `controller_name` to `url_name`

Codeigniter custom URL Routing

I have a URL in my Codeigniter
http://localhost:64743/index.php/mybox?name=John
I want it to look something like this
http://localhost:64743/index.php/mybox/name/John
is it posiible? how does custom URL works in CodeIgniter?
$route['mybox/(:any)'] = "mybox/index";
user above mention code in routes.php of config folder and you will get query string value by $this->uri->segment(3); and $this->uri->seggment(4);
for further assistance, please check
https://ellislab.com/codeigniter/user-guide/general/routing.html

Codeigniter - Htaccess : How to change a controller name

This is my first question here, so Hi all! ^^
I created a website with codeigniter framework and I have inserted a module for multilanguage. The url looks like this:
http://www.mywebsite.com/en/controller/function
The problem came when the client wanted to send a newsletter to all its customers in another language, but do not want to send the url with the controler with the name in english, because the newsletter is for spanish users.So:
URL is going to be send:
http://www.mywebsite.com/es/thecontroller
URL the client wants to be send ("elcontrolador" is "thecontroler" in spanish):
http://www.mywebsite.com/es/elcontrolador
I dont want to create another controler named "elcontorlador" only to show the same page as "thecontroler", because we don't want duplicate content for SEO purposes.
So, i want via .htaccess, a rule that when i type
http://www.mywebsite.com/es/elcontrolador
in the URL, mywebpage shows the info of
http://www.mywebsite.com/es/thecontroler
but with the URL
http://www.mywebsite.com/es/elcontrolador
(the controler "elcontrolador" doesnt exist).
So, is there any way to do this with the htaccess? I've tried it myself but I failed miserably i come here desperate, because I run out of time to deliver it and can not find a viable solution. I'll have to create the extra controller?
Need help D:
Maybe you can use the route config file instead to achieve this ?
$route['es/elcontrolador'] = 'es/thecontroler';
I don't know how you handle your multilangage, but you've got the idea.

ActionLink pointing to route not affected by current route

I have am problem because I have a menu where the links always should point to MyController/MyAction, but the site has other routes that go to the same action, but with more parameters.
Will give me the route but if I am currently on the same action and controller, but with more params these are included in the url as well.
Eg. if I am on the page:
http://localhost/MyWebsite/organizations/p001/departments/vest/employees/chn/MyController/MyAction
the url generated will be the same as above and not:
http://localhost/MyWebsite/MyController/MyAction
as i would like. Any suggestions?
Ahh found out that I could use RouteLink instead...

Resources