Rewrite URL in urlManager Yii Framework - yii

In file config main.php:
'/my-acc/<slug:.*>' => 'user/profile/<slug>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
But, when I create URL:
Yii::app()->controller->createUrl('user/profile', array('slug'=>'acc1');
Returns the result mydomain/user/profile?slug=acc1, without mydomain/my-acc/acc1.
Can somebody help me?

I think this should works:
'my-acc/<slug>' => 'user/profile',

Try this in urlManager rules
'my-acc/<slug:[a-zA-Z0-9-_]+>' => 'user/profile',
Modify regular expression according to your need.
To allow only digits
'my-acc/<slug:\d+>' => 'user/profile',
To allow string (without -,_)
'my-acc/<slug:\w+>' => 'user/profile',

if the above two answer works ,then ignore , other wise try this ,
have you added .htaccess file in this path ? , move the .htacess file where the root directory( .index.php file exist )
www
- index.php
- .htaccess
and in main.php file
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
...,
),

Related

Routing with regex in CakePHP#

I'm looking for help on setting up routes in CakePHP3.4.6 where
urls are variable. For instance, I want the following urls:
/California/Posts/view/Skateboard/Jan2nd/10
/Texas/Posts/view/Truck/Feb10th/35
to connect to
/Posts/view/10
/Posts/view/35
respectively. When doing so, I need the URLs preserved in the browser.
(i.e. browser URL shows /California/Posts/view/Skateboard/Jan2nd/10 while content is served for /Posts/view/10)
Can this be done through configuring routes.php?
Any advice will be most appreciated.
I tried using rewrite rules in webroot/.htaccess such as:
RewriteRule ^[^/]+/Posts/view/[^/]+/[^/]+/(\d+)$ /Posts/view/$1 [L]
But this just ends up in 404 errors. The pattern match seems to be correct as the following rule works:
RewriteRule ^[^/]+/Posts/view/[^/]+/[^/]+/(\d+)$ http://www.google.com [L]
Thanks,
Managed to figure this out.
$routes->connect('/:state/Posts/view/:title/:date/:id',
['controller' => 'Posts', 'action' => 'view'],
['id' => '\d+', 'pass' => ['id']]
Did the job

htaccess and multilanguage URL

I am trying to translate my URL, depending on the language selected by the user, e.g.:
www.example.com/en/discover or
www.example.com/fr/decouvrir
Obviously I want the two URL to point to the same page (where PHP handles the translation with a dictionary). I was wondering if there was a smart way in the htaccess file to do this "automatically" without having the write the rule for all the pages? For the moment I would do
RewriteRule ^en/discover$ discover.php?lang=en [NC,L]
RewriteRule ^fr/decouvrir$ discover.php?lang=fr [NC,L]
but this solution is not really the easiest if there are a lot of pages and if I want to add another language.
Thanks a lot for your help
You should pass all arguments to single php file (dispatcher), whitch would translate route and then include needed file. Something like:
.htaccess
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&include=$2 [NC,L]
PHP
// This is really bad code, just an example
$dictionary = array(
'en' => array(
'discover' => 'discover'
),
'fr' => array(
'decouvrir' => 'discover'
)
);
echo $dictionary[$_GET['lang']][$_GET['include']];

Apache mod_rewrite URL

Hi I need some help with mod_rewrite.
I would like to change this kind of URL
127.0.0.1/app/controller/<anyName1>/<anyName2>.php?firstParam=1&secondParam=text&...&nParam=nSomething
to 127.0.0.1/anyName1/anyName2/1/text/.../nSomething
Could you give me any example how to do that?
You can save yourself a lot of stress by not trying to modify anything beyond /<anyName1>/<anyName2>/ with mod_rewrite. Dynamically mapping path components to query variables requires some very funky recursive voodoo magic.
Instead grab only /<anyName1>/<anyName2>/, append the rest of the path to the redirect, and then use PHP to parse the path:
.htaccess
RewriteCond %{REQUEST_URI} !^/app/controller/[^/]+/[^/]+\.php
RewriteRule ^([^/]+)/([^/]+)/?(.*)$ /app/controller/$1/$2.php/$3 [L]
PHP
$components = array_filter(explode('/', $_SERVER['PATH_INFO']), 'strlen');
echo '<pre>';
print_r($components);
echo '</pre>';
Output:
Array
(
[1] => 1
[2] => text
[3] => ...
[4] => nSomething
)

Gii Unable to resolve the request "gii/index" - merge array understanding

Trying to access locally http://mysite.dev/gii but I'm getting:
Gii Unable to resolve the request "gii/index"
Here is my config/localdev.php file:
'modules'=>array(
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>false,
// If removed, Gii defaults to localhost only.
'ipFilters'=>array('127.0.0.1','::1'),
),
If I use this address:
http://mysite.dev/gii/default/login
But this a password protect one, and we defined as false.
We may think that localdev.php is NOT being applied, but I've echo "hello" inside the conditional that loads it, and it appeared".
Note:
Inside Yii framework there is a .htaccess file with:
deny from all
I don't see the reason for this behavior.
Can I have your help please ?
Update regarding the comments:
Here's the localdev.php requiring main.php
return CMap::mergeArray(
require_once(dirname(__FILE__).'/main.php'),
array(
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>false,
'ipFilters'=>array('127.0.0.1','::1'),
),
),
Here's the URL on main.php
'components'=>array(
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
),
'showScriptName'=>false,
),
IF we should add those:
'rules'=>array(
'gii'=>'gii',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
Should I add this and place all that it's on main.php rules OR, can we precisely add just those rules here on localdev.php ?
You should add the routes that service Gii to your app configuration as mentioned in the manual.

Redirecting subdomain to folder in Rails 3

I need to redirect help.mydomain.com/a-page to /pages/a-page without using Nginx or Apache redirects and only through Rails 3 routes.
The key here is the help subdomain determining that /pages/ should be dropped from the route although the view is under the /views/pages/
Any help is appreciated.
Try this:
match "/:page_name" => redirect("/pages/%{page_name}"), :constraints => { :subdomain => /help/ }