Suppose my application path is:-
http://www.example.com/index.php?r=dashboard/event/view
I make a new controller on which I want to change the URL to pretty URL.
Default URL for the new controller
http://www.example.com/index.php?r=newcontroller/view?id=23&name=urlpretty
I want to make above URL to pretty URL such as
http://www.example.com/23/sampleprettyurl
But I do not want to change the complete application URL path to pretty URL. My other controller path/URL should work normally.
Thanks in advance
I don't think is doable, controller name must be in the URL, get what you need to configure the module name, then the urlManager:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'newcontroller/<id>/<name>' => 'moduleAlias/newcontroller',
],
],
Then in the action you can get the value by Yii::$app->request->get('id');
in config web.php
component => [
/* pretty url */
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
/* ./pretty url */
]
and .htaccess on /web
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
Related
I have an action name cart in site controller and I have used pretty URL so my URL is
WWW.test.com/cart
But I want to redirect if someone enter WWW.test.com/cart/,
then it should be redirected to WWW.test.com/cart.
I don't have cart controller.
You need to use UrlNormalizer for that:
'urlManager' => [
// ...
'normalizer' => [
'class' => yii\web\UrlNormalizer::class,
],
],
See documentation about URL normalization for more info.
I use the Yii2 framework and want to remove site/index from my URLs.
Right now I have the following URL
example.com/site/index
but would like to change it to simple
example.com
I tried the following approach Yii2 How to remove site/index and page parameter from url but it doesn't work.
Could you please suggest how it can be done?
Looking at the question also asked at Yii2: Remove controller from URL
I ran into problems and I had to use the Yii2 resources as https://yii2-cookbook.readthedocs.io/enable-pretty-urls/
and this is what I got
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<action>'=>'site/<action>',
'<controller:[\w\-]+>/<id:\d+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+' => '<controller>/<action>',
],
],
You need to add rule for empty path at the beginning of your rules:
'urlManager' => [
'rules' => [
'' => 'site/index',
// rest of rules
],
],
I understand many variables outside what I can provide could be the problem, but I'm still asking if someone had this problem and could help.
Here's my UrlManager config in the components
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => false,
'showScriptName' => true,
'rules' => [
'' => 'site/index',
'member' => 'site/login',
],
],
This url works:
http://exampler.com/web/index.php?r=site/login
This url returns a 404
http://example.com/web/index.php?r=member
**NOTE: ** I don't have any messy Nginx or Apache rules on my server like this guy had. My rules seem to be completely ignored, whatever I write in them.
$rules is ignored if you set $enablePrettyUrl to false. From $rules documentation:
The rules for creating and parsing URLs when $enablePrettyUrl is true. This property is used only if $enablePrettyUrl is true.
https://www.yiiframework.com/doc/api/2.0/yii-web-urlmanager#$rules-detail
I'm developing a video hosting website. I have a small trouble with UrlManager in Yii config. Easch video in my db has a key like 't9d23f78ld' and I want the user to go to http://example.com/watch/t9d23f78ld to view the video. I have created the 'watch' controller and my urlmanager config looks like this:
'urlManager'=>array(
'class'=>'application.components.UrlManager',
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'<language:(ru|uz)>/' => 'site/index',
'<language:(ru|uz)>/<action:(contact|login|logout)>/*' => 'site/<action>',
'<language:(ru|uz)>/<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<language:(ru|uz)>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<language:(ru|uz)>/<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
),
),
My .htaccess looks like this:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
AddDefaultCharset utf-8
HOW DO SET UP THE URL MANAGER TO VIEW THE VIDEOS AT
Add rule:
watch/<id:\w+>'=>'watch/showVideo',
which mean, that id from url is passed to showVideo action.
Also you should add parameter in action:
public function actionShowVideo($id) {
...
}
I trying to enable pretty url in yii2 but it doesn't work as needed.
urlManager configuration:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'baseUrl' => '/',
]
public function actionIndex($custom_param)
{
print($custom_param);
}
example.com/mycontroller?custom_param=value works perfectly. But I need URLs such as example.com/mycontroller/value.
If you want to apply this toindex action of mycontroller and in case of custom_param is integer, add this to rules section of urlManager:
`urlManager' => [
'rules' => [
'mycontroller/<custom_param:\d+>' => 'mycontroller/index',
],
],
Otherwise you can modify pattern to fit your needs.
For example if custom_param is string, change d+ to w+.
If you want to apply this rule to other controllers, you can do it like this:
'<controller:(mycontroller|anothercontroller)>/<custom_param:\d+> => '<controller>/index',
Read more in official documentation:
Guide to url rules
Guide to parameterizing routes
UrlManager $rules
In your web.php file below Components use this code:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
Create A .htaccess file in your web folder and paste it:
RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # therwise forward it to index.phpRewriteRule . index.php