Redirect url for REST service - apache

I am able to reach my website at a certain ip address and I am going to implement a REST service. I have some PHP files that perform actions on a database and I am calling them from the client. I am using linux ubuntu as server and so far I can do this:
http://xxx.xxx.xxx.xxx/api/create/?id=someId&val=someValue
http://xxx.xxx.xxx.xxx/api/delete/?id=someId
I can do the above because inside /var/www/html I have a folder called api that contains another folder called create. The create cointains the file index.php so that I can omit it and execute the URL you can see above.
This works fine but I don't think this is the proper way to do it. I am new with this so I don't know what to do. After some researches I have found that my goal probably be achieved using an .htaccess file use url rewriting but I am not sure.
How can I do this? Do I have to place all the php files in a single folder and then use an htaccess file? (^)
(^) To be more precise: instead of having this
http://xxx.xxx.xxx.xxx/api/create/index.php?id=someId&val=someValue
http://xxx.xxx.xxx.xxx/api/delete/index.php?id=someId
//and so on with other actions...
Do I have to create a folder like
http://xxx.xxx.xxx.xxx/files/
containing all my php files (create.php, delete.php, view.php...) and the use an htaccess to redirect?
I see that websites offer their api using www.domain.com/api/something/?data=Value or www.domain.com/api/something/dataAbout/. Are they doing what I have said about the .htaccess? I hope I have well explained my problem.
htaccess:
RewriteEngine On
RewriteRule ^api/([\w-]+)/?$ files/$1.php [L,NC]
This is inside /var/www/html and I have api inside /home/username/api .
Thanks Emma

Do it like this:
Create php files in a folder files/ subdirectory as create.php, delete.php, view.php etc (by renaming each individual index.php file, you mentioned).
Move away api directory somewhere outside site root.
Once that is done use following .htaccess file in /var/www/html/:
RewriteEngine On
RewriteRule ^api/([\w-]+)/?$ files/$1.php [L,NC]
Then use new URLs as:
http://xxx.xxx.xxx.xxx/api/create?id=someId&val=someValue
http://xxx.xxx.xxx.xxx/api/delete?id=someId

In first, this is not the right way to create a RESTFUL API. My suggestion is to you read a best practices article.
You shouldn't create a CREATE and DELETE folder. You should use HTTP actions.
To create a new record you should use POST. In example, POST /user and in the body you pass the user's information.
In another example, you could use the same route by using different HTTP methods: DELETE /user/1 to delete a user and PATCH /user/1 to edit some already existent user's information.
Hope it's help you.

Related

htaccess and IIS rewrite to map subfolder to one level up

I have this single page app made using AngularJS and it resides in the document root: http://example.com/
The API that Angular interacts with is created with CakePHP and is located in a subfolder called api. It's accessed via http://example.com/api/xxx.
Apart from acting as an API, the CakePHP application also has an administration backend. In the current URL structure, it's accessible via http://example.com/api/admin. To make it look neater I want users to be able to access it via http://example.com/admin.
What htaccess and IIS rule will I need to achieve this? I'll be testing on both platforms.
Thank you.
You mean http://example.com/admin I assume.
In htaccess you can do it like this:
RewriteEngine On
RewriteRule ^api/admin(/?$|/.+) /admin$1 [QSA,L]
Demo here: http://htaccess.mwl.be?share=d8c07577-d6fb-501d-8e32-403531d471a1

Magento REST is not Found after http://{magentoHost}/api/

I am trying to access rest API for Guest User through
http://localhost/b_marketing/api/rest/products, but the page redirects to 404 page. I have checked retrive catalog products in ACL attributes RULES, REST - attributes and REST - ROLES, correctly. It also works correctly on my other project by using these all configurations.
There is one thing I need to mention here that my project works only when I use http://localhost/b_marketing/index.php , despite of setting up the htaccess rewrites and setting rewrite from admin panel. My apache rewrites are also set.
When I use http://localhost/b_marketing/api/?wsdl it works and some xml returns.
But I cannot use http://localhost/b_marketing/api/rest/products
The Response Header is always 404.
I also cannot access REST API using http://localhost/b_marketing/index.php/api/rest/products as well.
Make sure you have rewrite rule in .htaccess.
Go to magento root folder, in your case it is b_marketing
Open .htaccess file in any editor
Locate rewrite rule for api
If you don't have .htaccess file in place, you may consider to copy fron original
Magento packagem
If you run nginx:
.htaccess is not supported. Edit the nginx configuration and add the line:
location /api {
rewrite ^/api/rest /api.php?type=rest last;
}
I read that that line sometimes is already there however with the the word break instead of last. Thats wrong, change it to last.
Make sure to use -MultiViews. As the api.php and /api path have the same name (without extension), Apache might resolve api automatically to api.php and cause trouble.

Multiple Domains to Display Content from Landing Pages on Another Domain

We have created a bunch of landing pages on a Joomla CMS system, such that the URL for each landing page is www.domain.com/page1.html and www.domain.com/page2.html, and so on. Of course the page1.html isn't really an HTML file it is a dynamic CMS page, just rewritten with htaccess.
The goal is to have one of our other domains, something like www.uniquedomain1.com show the content of www.domain.com/page1.html. Or, another domain like www.uniquedomain2.html show the content of www.domain.com/page2.html.
This needs to be search engine friendly so we can't use URL masking. Also we can't use HTACCESS redirects as this actually changes the URL in the browser bar. Need to keep the www.uniquedomain1.com URL in the browser bar.
Tried Apache VirtualHost options without any luck. You can park in a directory but not from a URL.
Ended up parking the domains on one folder, and then creating a PHP script to detect the domain host and then use CURL to query the correct url and deliver content. This whole thing seems ridiculously over complicated, and of course CURL isn't the best option, but it is all we could get to work.
Any thoughts on how to do this, or a better solution?
You can use HTACCESS redirect rules to do it without performing a redirect.
Change the html file names to be the domain name of the desired domain like domain.tld and do something like this in an .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9\.-]+\.[a-z]+) [NC]
RewriteRule ^$ /%1.html [L]
A quick test of this worked for two of my test (sub)domains test.domain.tld and test2.domain.tld. Both properly redirected to files with the names test.domain.tld.html and test2.domain.tld.html without modifying the URL.
You could also just use your PHP wrapper script to grab the content of each of the miscellaneous html files and output them.
If you renamed all of your HTML files (as in my previous suggested answer) to be domain.tld.html you could do it fairly easily. Something might look like:
<?php
require($_SERVER['SERVER_NAME'] .'.html');

redirect directory to new address

hi i want redirect my directory to new address and remove .html ext from end
example:
http://www.mysite.com/viewdownload/11-e/652-elena-2010.html
redirect to
http://www.mysite.com/download/viewdownload/11-e/652-elena-2010
or
http://www.mysite.com/viewdownload/9-b/1281-birdsong-2012.html
redirect to
http://www.mysite.com/download/viewdownload/9-b/1281-birdsong-2012
Assuming mod_rewrite is installed, just use URL rewriting in your .htaccess files. You can use URL rewriting to redirect users as well as doing an internal transfer.
A good reference is here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Note that if there is any custom logic to mapping your old URIs to your new ones you might want to rewrite the old URIs to a PHP script which then returns the needed file.

Dynamic URLs + SEO

I have a website that generates dynamic urls for all content.
It uses a function to show the content on index.php page and it's called by "action".
So, if the user wants to signup, there is a signup function calling it.
If the user want's to browse downloads page, there is a downloads function calling it.
Something like this:
index.php?action=signup
index.php?action=downloads
index.php?action=news&type=type&id=id
In the last case are the news.
The news are divided by type and ID, both numerics.
Type can be: article, update, etc.
My question is: HOW and WHAT must I do in order to transform it all on friendly URLs?
Presuming you are on an apache server, you will want to use a .htaccess file, and a method of using rules to rewrite URLs, not surprisingly called RewriteRule.
There's a ton of information through google if you search for .htaccess RewriteRule, but a great starting point is http://corz.org/serv/tricks/htaccess2.php.
You will essentially end up by putting a file called .htaccess in your public web files folder, with something like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(?:/(.*)){1,3} index.php?action=$1&type=$2&id=$3 [R,NC]
Note: I've not tested the above, and it will differ depending on how you want your pretty link to look.