ApiRest Beautify url with rewrite paramaters - php

I'm building a RestApi and I have a problem with urls. I want to transform "api.php?request=users" into "api/users". But I also want it to be dinamic. For example: If a user types "api/users/13" I want it to be tha same as "api.php?request=users&id=13". Is there any way to make it dinamic? Do I have to create every case individually? How could this be done in each of these two cases? Thanks so much in advance for your help!!

the best way is change .htaccess file like this:
RewriteEngine On
RewriteRule ^api/([^/])/([^/])/([^/]*)$ /api.php?request=$1&id=$2&state=$3 [L]
The original URL:
http://www.domain.com/api.php?request=mike&id=3&state=active
The rewritten URL:
http://www.domain.com/api/mike/3/active
update: if you don't know the number of parameter add one more rewrite for each 'level':
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1 [L]
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1&id=$2 [L]
RewriteRule ^api/([^/])/([^/])/([^/]*)$
/api.php?request=$1&id=$2&status=$3 [L]
Regards.

I think you must add a new RewriteRule, because you need different variables.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)/?$ $1.php?request=$2
RewriteRule ^(.*)/(.*)/(.*)/?$ $1.php?request=$2&id=$3

Related

Mod rewrite and dynamic URLs

i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.
Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]
Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]

how to retrieve a specific get data in htaccess and pass it on rewrite rule

I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.

create SEO friendly URL using rewriterule

I try to rewrite my SEO URLs to some real GET requests, to handle in my PHP file.
I want to have these 2 cases to work:
mysite.com/company-profile -> index.php?action=company-profile
mysite.com/faq/howcanijoin -> index.php?action=faq&anchor=howcanijoin
I got the first case to work using the rule:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?action=$1
For the second I tried also. I put this rule before the previous one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2
But it's not working. Any suggestions? If I understand correctly inside each parenthesis goes variables $1, $2 etc?
Do this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?action=$1&anchor=$2
This?
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2 [QSA,L]

extending a htaccess rewrite rule

I have following rewrite rule/condition in my htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^id([0-9]+)/?$ /profile.php?user_id=$1 [L]
The result is "example.com/id1"
Now the problem I have, I need to put a $_GET parameter sometimes to the URL, but actually they get ignored, example:
example.com/id1?section=video
example.com/id1?section=photo
example.com/id1?section=audio
Also not working:
example.com/id1&section=video
Is there a chance to accomplish this task by extending the htaccess rule? Remember that the URL not always have the $_GET parameter added.
Best regards!
Try using the QSA flag
RewriteRule ^id([0-9]+)/?$ /profile.php?user_id=$1 [L,NC,QSA]

shorten url problem using htaccess

I need to change
http://localhost/engineering/management/administrator/modules/course/view.php
to
http://localhost/engineering/course_view.php
using htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.php$ management/administrator/modules/$1/$2.php
RewriteRule ^(.*)_(.*)\.php$ management/user/modules/$1/$2.php
Works fine for this,
But for the below url its not working
http://localhost/engineering/management/user/modules/data/edit.php
Please try with this code:-
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.php$ management/administrator/modules/$1/$2.php
RewriteRule ^(.*)_(.*)\.php$ management/user/modules/$1/$2.php
Hope it helps.
when you use this one!
RewriteRule (.*)_(.*)\.php management/administrator/modules/$1/$2.php
because it's possile ro rewrite other URLs(that you've mentioned), you can add special part to it to avoid such thing. e.g:
RewriteRule swd(.*)_(.*)\.php management/administrator/modules/$1/$2.php
and url:
http://localhost/engineering/swdcourse_view.php
and for second one, use other characters than swd!!
RewriteRule ^usr(.*)_(.*)\.php$ management/user/modules/$1/$2.php

Categories