URL rule for dynamic suffix in Yii2 - regex

I want to receive a parameter from URL with extension in that.
Here is my controller:
public function actionIndex($directory=null,$filename=null)
{
echo $directory.$filename;exit;
}
Here is my URL rule:
'<directory:\w+>/<filename:\w+>' => 'file/index',
it works like that:
localhost/uploads/abc
But it shows 404 when I do this:
localhost/uploads/abc.pdf
Any suggestions how this can be achieved?

I was able to achieve that with following regex:
'<directory:\w+>/<filename:\w+[.]\w+>' => 'file/index'

Related

How can I get a param using regex in a nginx server block?

I'm trying to get a param(h=1500 for example) via regex in a server block in an nginx server but it's not working. My last try was this:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)?h=(?<height>\d+)$" { ...... }
Here you can check and it works: https://regex101.com/r/kP9eY9/1
But in my server block file it does't.
If I try something like this, it works:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)/(?<height>\d+)$" { ...... }
Instead a param like "h=300", I just use a "/300" and I can get the value in my server block file.
I'm not a expert using regex so I can't see if there is something wrong. I need your help guys! Thank you!
From the documentation:
locations of all types test only a URI part of request line without
arguments
which means the ? and anything that follows it.
As #Richard mentioned, you can't use request arguments in locations regexps.
If you need to work with request arguments in your nginx config you might use $arg_ and/or $args syntax:
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args
I.e
location / {
if ($arg_param = 'someval') {
# some code here
}
}

How can I create an Express route that handles URLs that look like this?

app.get('/:service[SOMETHING GOES HERE]', function(req, res, next){
console.log('Service is:', req.params.service);
});
This needs to catch URLs that can look like any one of:
/foo
/foo/bar
/foo/bar/baz
The call back isn't concerned with anything that comes after foo, but ideally should be able to access foo as a parameter called service without having to manually parse the path.
I've been using this to test and still haven't found anything that does exactly that. Closest so far is /:service*.
Edit: No it's not a duplicate of the one where the answer is /:service/* because that doesn't cover /foo.
Using /:service* in actual Express routes does exactly what you want:
/foo maps to { '0': '', service: 'foo' }
/foo/bar maps to { '0': '/bar', service: 'foo' }
/foo/bar/blah maps to { '0': '/bar/blah', service: 'foo' }
The Express Route Tester, for some reason, maps these URL's differently for that these kinds of patterns (it might be configured differently than Express).
You can use the app.use() function for that. Read the doc about path handling for more info. Your code once modified will be:
app.use('/foo', function(req, res, next){
console.log('Service is:', req.params.service);
});
The downside is that you are not going to recover foo as the service parameter.

route query for play framework 1.2.5

I need to add a route for the following syntax:
http://www.testsite.com/select?term=query1
In my routes file, I tried using the following
GET /select/{term}
However, the above does not catch the URL - instead it goes to another handler in the config (placed beneath the handler for select/{term}:
GET /{auth}
Any thoughts on fixing or troubleshooting this would be most welcome. thanks
?term= means that term is a parameter - not part of the route you are trying to match
so you'd write
GET /select YourControllerClass.yourMethod
....
YourControllerClass extends Controller {
public static void yourMethod(String term){
Logger.debug("term=" + term);
}
}
If your URL was http://www.testsite.com/select/query1 then the route definition you provided above should work

Manipulate reg expression variables in .htaccess

I want to redirect the client from
http://mydomaine/cat?start=x
to
http://mydomaine/cat/page-(x divide by 15)
e.g from http://mydomaine/cat?start=30
to
http://mydomaine/cat/page-2
There any way to do that with .htaccess?
Not sure you can do this in .htacces file.
But, if you are under Joomla (from the tag of your question) you can create a system plugin which will let you manipulate urls.
Here is the doc from joomla.org Documentation
I think this tip only work with joomla 2.5.
Thanks all of you, so this the solution that is working for me:
if (preg_match("/(.*)\?limitstart=[0-9]+/", JRequest::getURI(), $matches)) {
header('Status: 301 Moved Permanently', false, 301);
header("Location: ".$matches[1]);
exit();
}

URL routing problem in codeigniter

I am trying to route a URL using codeigniter URL routing.
I want to redirect a url like
/users/edit?email to userController/editemail
/users/edit?password to userController/editpassword
I tried using the following line in routes.php in config folder
$route["users/edit?(email|password)"] = "userController/edit$1";
This displays page not found. I am guessing that ? is being treated as a regular expression character. I tried escaping it but that didn't work either.
I don't want to set the config setting uri_protocol to PATH_INFO or QUERY_STRING, since this is just a pretty URL I want to setup, not pass anything to the action.
Can somebody help me out over here?
Regards
You should escape the ? like this, it should work. (not tested)
$route["users/edit\?(email|password)"] = "userController/edit$1";
Later edit:
This works as intended:
$route["users/edit(email|password)?"] = "userController/edit$1";
The userController looks like this
<?php
class UserController extends Controller {
function edit()
{
echo "general edit";
}
function editemail()
{
echo "edit email!";
}
function editpassword()
{
echo "edit password";
}
}
Router works like this:
if you go to http://sitename/index.php/users/editemail you see editemail() action.
if you go to http://sitename/index.php/users/editpassword you see editpassword() action.
if you go to http://sitename/index.php/users/edit you see the edit() action (the question mark makes optional the email/password field and you can do some other things in edit() action