1

I want to make my first api, but im having trouble setting up the urls. The api url is here:

http://tools.fifaguide.com/api/

So for example if some one goes to:

http://tools.fifaguide.com/api/player/messi

Then I need this page to be loaded:

http://tools.fifaguide.com/api/server.php

What do I write in .htaccess? and where should I put it?

This is what I have right now:

RewriteEngine On
RewriteRule ^api http://tools.fifaguide.com/api/server.php

But it doesnt do anything, also the htacces file is in the api folder. Any help would be really apreciated!

Steven Baltay
  • 534
  • 2
  • 5
  • 15

2 Answers2

6

This is what ended up working for me

<IfModule mod_rewrite.c> 
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ api/index.php [QSA,L]

////// wordpress stuff /////////
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L]
</IfModule>  
Steven Baltay
  • 534
  • 2
  • 5
  • 15
3

You might try this:

RewriteEngine On
RewriteRule ^api/(.*) http://tools.fifaguide.com/api/server.php [R,L]

And if you need the resulting url you could add this:

RewriteEngine On
RewriteRule ^api/(.*) http://tools.fifaguide.com/api/server.php?r=$1 [R,L]

Then in your script you could access the requested url with $_GET["r"] (assuming php...)

Also a helpful tool i've found for htaccess: http://htaccess.madewithlove.be/

-Ken

Ken Koch
  • 426
  • 3
  • 12
  • Thanks ken. It works in http://htaccess.madewithlove.be/ but not in my site. what loads is http://tools.fifaguide.com without any images. Could this mean theres other htaccess files interfering? – Steven Baltay Jun 14 '13 at 17:55
  • its possible, you can just check the directories for other htaccess files. [This post](http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules) has a lot of good tips for debugging htaccess. Can you see what url the images are requesting? Also you can try removing the L from [R,L] (the L tells mod_rewrite to stop rewriting after that is matched) – Ken Koch Jun 14 '13 at 18:27
  • Ok so i figure out that that wordpress is redirecting 404 pages to my index.php... but when I disable it and go to http://tools.fifaguide.com/api/clients/jim I still get a 404 error – Steven Baltay Jun 14 '13 at 18:35
  • hmm so Wordpress is involved, you have to be careful then. Wordpress has an htaccess which does something similar to what your trying to do except it uses it to rewrite everything to an index.php where it routes everything else from. You'll have to add your rule to there but make sure its in the right place. [See this](http://randomtype.ca/blog/the-wordpress-htaccess-file-explained/) link for a line-by-line explanation of the WP htaccess file. I haven't done too much with it but you may be able to set a different rewrite base for the /api requests and direct them into your rewrites. – Ken Koch Jun 14 '13 at 18:40