I am setting the pagination variable with the following:
$dp->pagination = array('pageVar'=>'page');
And adding to the main config rule with this:
'/myrule/page/<page:\d+>' => 'controller/action'
URLs /myrule/page/1 and /myrule/?page=1 work. However, pagination generates /myrule/?page=1. How do I get URLs like /myrule/page/1 from pagination?
Resolve: displace rule higher on the list - that rule worked before the other.
Related
I want to make user-friendly API for my service. I am trying to make it like Discord API - https://example.com/api/accounts/<account ID>/<stuff>/, some expected link examples below:
https://example.com/api/accounts/1234567890/data/
https://example.com/api/accounts/1234567890/inventory/
https://example.com/api/accounts/1234567890/servers/
I tried to make some htaccess tricks, but without any result - website got corrupted (just white page without anything) or account ID was considered as folder (HTTP 404, because I don't want those IDs as folders).
Thank you in advance for your response
Edit 1:
File structure can be really any, I just want it working. But most expected by me would be
api
- accounts
- data
- inventory
- ...
- server (example)
- game (example)
Edit 2:
My API is OOP - so I can call it from any place. I just need to get data and create new object and call some functions. In my bad-looking API, link was like: https://example.com/api/accounts/data.php?id=123456789 and in that script I call $stuff = new StuffObj($id, $other, $stuff, $from, $post); and function like $stuff->execute();. But with new API I can just make GET var, like action and in switch choose what object to use.
In my bad-looking API, link was like: https://example.com/api/accounts/data.php?id=123456789
In that case, to implement a "pretty" URL of the form /api/accounts/<id>/data/, you would do something like the following using mod_rewrite in your /api/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteRule ^accounts/(\d+)/data/$ accounts/data.php?id=$1 [L]
To make this more "generic", to rewrite a URL of the form /api/accounts/<id>/<action>/ to /api/accounts/<action>.php?id=<id>, assuming <action> would only consist of lowercase letters then you could change the above RewriteRule to read:
RewriteRule ^accounts/(\d+)/([a-z]+)/$ accounts/$2.php?id=$1 [L]
Although in your file structure you appear to have "subdirectories"(?) of the /accounts directory that also match the basename of the .php file? If so, this could be problematic, hence why I have disabled MutliViews above.
But with new API I can just make GET var, like action
Yes, you could - and would perhaps be preferable - everything is managed by a single entry point (eg. index.php - but you can call it anything your like) - However, you need to be specific and decide on this before implementing the "pretty URL" - which is really just cosmetic fluff. In fact, if this is just an API, do you need to implement a "pretty URL" at all?
We have a social networking web site for students. Our url looks like this domain.com/student/145236 where 145236 is the roll number. We now have an option of doing something like this domain.com/student/145236/student-name but since google / search engines would prefer domain.com/student-name/student/145236 we would like to do like this, but we are unable to do it, we tried doing this using routes $route['(:any)/student/(:num)'] = 'student/$1'; but it shows 404 page, any help would be grateful :)
This should be work for you:
$route['(:any)/student/(:num)'] = 'student/yourfunction/$1/$2';
Here $1 is for ist param (:any) and
$2 is for second param (:num) .
For getting values from the URL inside the student controller you can use CI Segments.
I am working a codeigniter project. for example I have a controller Blog and a method category with param.
When i go to a link
Developer Blog
it works fine. which redirect to a page developer blog.
Is it possible to rewrite the url as domain.com/blog/developer-blog dynamically ? Thank you.
Open your application/config/routes.php and try following (if you want exact as mentioned in question)
$route['blog/developer-blog'] = 'blog/category/2';
$route['blog/user-blog'] = 'blog/category/3';
for dynamic routing you may use this
$route['blog/developer-blog/(:num)'] = 'blog/category/$1';
$route['blog/user-blog/(:num)'] = 'blog/category/$1';
so your URL now should be looks like https://domain.com/blog/developer-blog/1, https://domain.com/blog/developer-blog/2
or https://domain.com/blog/user-blog/1, https://domain.com/blog/user-blog/2 etc
for more see http://www.codeigniter.com/user_guide/general/routing.html
For my current project in codeignitor I needed to make user profile like this
http://domain.com/userid
Then I tried to add this in router.php
$route['(:any)'] = 'profile/user/$1';
Which is working fine. Now I want to make another URL for language like this
http://domain.com/es
http://domain.com/fr
As for both url uri segments are first, when I type
http://domain.com/es
I see the page of
http://domain.com/userid
I am using .htaccess file for removing index.php in codeignitor. Is there any help how can I achive this task in making shot url for multiple controller. Either with .htaccess or router.php?
Because the routes system works from the top down, if you have multiple rules that can match a url, it picks the first one. So you could do:
$route['(es|fr|en)'] = 'language/$1';
$route['(:any)'] = 'profile/user/$1';
If the first rule matches, it runs, otherwise it tests the profile rule.
You will definitely continue running into issues though with that profile rule, and it would be easier if you did something like:
$route['users/(:any)'] = 'profile/user/$1';
That way it would be more clear what the url is doing, and it will help you for when you are writing rules in the future.
I'm trying to wrap my head around rewriting some urls internally in Joomla! 1.7 with SEF features turned on but can't seem to figure it out:
The following SEF URL exists (menu item):
website.com/local/amsterdam
What I would like is the following:
http://website.com/local/amsterdam/trends (non-existant)
to render
http://website.com/local/amsterdam?show=trends
while still displaying the first URL.
Working with .htaccess the following works (but doesn't show SEF URL):
RewriteRule ^local/amsterdam/trends$ index.php?option=com_content&view=article&id=14&Itemid=176&show=trends [L]
But this doesn't:
RewriteRule ^local/amsterdam/trends$ local/amsterdam?show=trends [L]
I'm hoping to find a solution without having to use an id so that it will dynamically render the correct page for all cities. I'ld appreciate any thoughts on doing this in .htaccess as well as any different solutions to achieve this! Thanks in advance.
Although I was initially searching for a mod-rewrite solution I have figured out a way to achieve the same by modifying the (core) Joomla! router.
On line 47 of includes/router.php after:
$path = substr_replace($path, '', 0, strlen(JURI::base(true)));
I added the following:
$subpages = array("trends","other"); //Add URL segments you want to reroute
foreach ($subpages as $subpage):
if (strstr($path, "/".$subpage)) :
$path = str_replace("/".$subpage, "", $path);
$vars['show'] = $subpage;
endif;
endforeach;
Now when loading http://www.website.com/local/amsterdam/trends, this URL is displayed while the page http://www.website.com/local/amsterdam is actually loaded with the ?show=trends parameter.
For me this a more flexible solution than using mod-rewrite even though a core file is modified. You might want to use some conditional statements to only run this code in certain conditions. Hope it helps.