Pass parameter to default controller/method with Yii urlManager - .htaccess

I would like to use a catch-all rule for urlManager that would pass anything after my base url to a default controller and method as parameters.
My goal would be for a url such as mysite.com/123 to map to mysite.com/controller/method/123 where controller/method are predetermined and 123 is passed as a named parameter.
Such a rule would be put last in the urlManager chain so that if none of the other rules match it would pass whatever is after the base url to my selected controller/method.
Any ideas??
Edit:
Adding a rule '<id>'=>'controller/method' (which I think I had tried anyhow) and then viewing site.com/123 would return a 404 not found, but from apache, NOT Yii. Something I did not take into consideration.
Going to mysite.com/index.php/123 got the desired result. Going to mysite.com/controller/method though would route the url properly. Strange...

Yes, you have to put this as the last rule under all other rules.
'<id>' => 'controllerName/methodName/<id>,'
Example:
'<id>' => 'user/view/<id>',
This will redirect all URLs like this:
mysite.com/1
To:
mysite.com/user/view/1
If you want to restrict to numbers only, use
'<id:\d+>' => 'controllerName/methodName/<id>,'

You should add this rule to bottom of url rules:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<pname:\w+>'=>'site/test',
),
),
Pname: your named parameter.
Site/test: the target action.
In your action you should define your "pname" as method paramter:
public function actionTest($pname) {
echo "Name:$pname";
}

Related

How to redirect a wrong url with the same pattern that a user messed up in django, without javascript

assume the following:
Your model has: Products.slug
urls: path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
views: products = Products.objects.get(id=id, slug=slug)
Someone goes to /products/1/brazil-nuts/, and it goes to the right page.
But then someone copy/pastes the url wrong to: /products/1/brazil-nu/
Then you get a DoesNotExist error... Now you could "fix" this by not forcing the slug=slug argument in your query, but then the url is incorrect, and people could link from /products/1/brazil-nu-sjfhkjfg-dfh, which is sloppy.
So how would I redirect an incorrect url to the correct one with the same url structure as the correct one (only id and slug arguments), without relying on JavaScript's window.history.pushState(null, null, proper_url); every time the page loads?
I've already tried making the same url pattern to redirect, but since django uses the first pattern match, it won't work no matter where you put the url in your list.
Just update your view:
products_q = Products.objects.filter(id=id, slug__startswith=slug)
products_count = products_q.count()
if products_count == 1:
product = products_q.first()
return reverse_lazy('product_detail', args=[product.id, product.slug])

Silex optional locale route

I might be approaching this the wrong way so I am open to alternatives.
I would simply like to match the following sample urls using a single route:
/
/welcome
/en/welcome
/fr/welcome
/my/arbitrarily/deep/path
/en/my/arbitrarily/deep/path
/fr/my/arbitrarily/deep/path
etc
Here is what I have so far:
$app->get('/{_locale}{path}', function (Request $request) use ($app) {
$path = $request->attributes->get('path');
// do stuff with path here
})
->value('_locale', 'en')
->assert('_locale','^(en|fr)?$')
->value('path', 'index')
->assert('path', '.*')
->bind('*');
Now this seems to work as expected, but when I try to use the twig path() or url() it fails to build the correct url, for example:#
on /foo (no locale specified on the url so defaults to en):
{{ path('*', {path:'foo/bar'}) }}
will result correctly in
foo/bar
on /fr/foo, the same call:
{{ path('*', {path:'foo/bar'}) }}
results in
frfoo/bar
This is because of the missing / between {_locale} and {path}, but by changing the route to:
/{_locale}/{path}
It stops matching /foo and only matches either /en/foo, /fr/foo or //foo.
I'm not sure where to go from here :s
I didn't want to use multiple routes (maybe one with and without a {_locale}) because I'm not sure how that works with the path() function, I basically want the result of path() to include the current locale in the url if it's not 'en' (I think is what I'm getting at).
Can anyone help me with this?
Cheers
Toby
Declare a route for /{_locale}/{path} and /{path}.
As for path() , since you defined a default _locale value for the first route , there should not be any issues in your views.

Codeigniter 2.1 and .htaccess - rewrite url

I need to rewrite this url:
domain.com/mali_oglasi/index/1(any number)
to:
domain.com/mali_oglasi
In my .htaccess file I have this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
How can I do this?
If the only thing you want is to map your controller/method differently than the default behaviour, you can use the route.php config file. See the official documentation here : http://codeigniter.com/user_guide/general/routing.html
In your case you'll have something like this :
$route['mali_oglasi/index/(:num)'] = 'mali_oglasi';
Later in your controller you can still get the original digit by using :
$this->uri->rsegment(3);
instead of :
$this->uri->segment(3);
(see official documentation here : http://codeigniter.com/user_guide/libraries/uri.html )
EDIT:
In fact, if you just wish to get rid of the "index" segment when you need to add parameter, you may want to do the inverse of my first answer :
$route['mali_oglasi/(:num)'] = 'mali_oglasi/index/$1';
With that line, every request in the form of "www.yourdomain.com/mali_oglasi/1" will be interpreted by codeigniter as if it were "www.yourdomain.com/mali_oglasi/index/1". Meaning the method "index" of the controller "mali_oglasi" will be used to handle your request.
If you need to retrieve the digit, you want to use :
$this->uri->segment(3);
So if your client should ever go to the url "www.yourdomain.com/mali_oglasi/index/1" directly, you will still retrieve the good uri segment. ( $this->uri->segment(n); give you the n-th segment after route.php rewrite the uri, and $this->uri->rsegment(n) give you the n'th segment before the uri is rewritten. )
I suggest to redirect the user to the new URL :
in your controller mali_oglasi >> in the function index
put the below line
redirect('mali_oglasi');
e.g.
class mali_oglasi extends CI_Controller{
function Index($id){
// Note : make sure you have loaded the url helper
redirect('mali_oglasi');
}
}
Note: don't forget to load the url helper
Note: Set the $config['index_page'] = ''; instead of index in application/config/config.php

CodeIgniter - htaccess - Redirect

I am currently working with "Code Igniter" and the "i18n Multi-language Library Helper". My website is bilingual.
The simple task I want to do is to force redirect of this path:
domain.com/inscription
to
domain.com/fr/inscription
I tried to do it with CI Route Engine, but it it not working properly because the route engine will redirect to the current language ( ex. domain.com/en/inscription ), which should not work. Only domain.com/fr/inscription should work.
I believe the best way to do it if with the htaccess file, but I can't get it to work.
If you are using Codeigniter 2.x try using this library
http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n
Read the guide on how to set up the library and you can see there in MY_Lang.php file array like this. The first language in the array is the default one. So it will redirect you automatically to the default language
// languages
private $languages = array(
'en' => 'english',
'de' => 'german',
'fr' => 'french',
'nl' => 'dutch'
);
Hope this helps
Here is a code to redirect
# This allows you to redirect index.html to a specific subfolder
Redirect /inscription http://domain.com/fr/inscription
the line under # comment line is the code you need to paste

HTACCESS - Block everything but specified SEO friendly URL

I haven't found all the answer to my current problem.
Here is the root of the site:
cache
img
display.php
admin.php
What I need is to block all the direct access of the files and allow only access via url formatted like that:
1 ht*p://sub.domain.com/image/param/size/folder/img.jpg (param, size, folder, img are parameters)
2 ht*p://sub.domain.com/action/param1/param2/ (param1, param2 are parameters)
1 would point to display.php with the correct parameters
2 would point to admin.php with the correct parameters
Every other access must be 404 (at best) or 403
my rules are (the htaccess is in ht*p://sub.domain.com/):
RewriteRule ^image/([^/]+)/([0-9]+)/([^/]+)/([^/]+)\.jpg display.php?param=$1&size=$2&folder=$3&img=$4 [L]
RewriteRule ^action/([^/]+)/([^/]+) admin.php?action=$1&param=$2 [L]
Those rules work as I want to but I am stuck on how to block any access that does not come from those URL!
Also (as a bonus) I would like to be able to use the same htaccess on diferrent web address without having to change this file.
Thanks in advance
Have you try moving the image out of the public folder and use php to call the image in?
For the PHP files you can use the switch statement (http://www.php.net/switch).
For the admin.php file you can do something like:
$get_action = $_GET['action'];
switch ($get_action) {
case "edit":
case "view":
case "delete":
case "add":
//Continue loading the page
break;
default:
header('HTTP/1.1 403 Forbidden');
die();
}
Note: I don't know how your code looks or works, but you can have an idea base on the code I added.

Resources