How to create mod rewrite rule for simple api - php

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!

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>

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

Related

Friendly Url don't get the link [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

PHP redirect to another page with dynamic url and get data [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

Removing File URL with .htaccess

I am writing a program and run it locally with xampp. the full path to the program is http://192.168.101.103:7777/WeltesMaft/index.php
I am writing a .htaccess in C:\xampp\htdocs\WeltesMaft\
containing this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://192.168.101.103:7777/WeltesMaft/%1 [L]
but somehow my htaccess doesnt work. it still showing like this
http://192.168.101.103:7777/WeltesMart/_resources/main.php
which is in index.php i am including main.php. So when user enters index.php, it will redirect to main.php in an instant.
I want to hide it so that the url looks like this,
http://192.168.101.103:7777/WeltesMart/
Please help me what am i doing wrong here...
You can use this code also.....
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _resources/main.php/$1 [L,QSA]
</IfModule>
Change RewriteBase to your directory name and change the htaccess rule as following and try again:
RewriteEngine On
RewriteBase /WeltesMaft
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule / index.php [L]

Htaccess redirect not successfully working

I am currently creating a site in localhost and have no luck in getting redirect working. I want to redirect
http://localhost/s/profile.php?uid=16
to
http://localhost/s/16
How could I go about doing this. I have tried using examples online with no luck.
Edit: I tried
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /s/profile.php?uid=$1 [L]
Using copy and paste from online. I always try to figure out who to use htaccess but can't seem to do so.
Here is the base functionality you are looking for. This is just a simple example and you should certainly do your research on this subject.
Alternatively, there are plenty of generators.
http://www.htaccessredirect.net/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile\.php$ - [L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /profile.php [L,NE]
</IfModule>
You can have this rule
RewriteRule ^s/([^/]*)/?$ /s/profile.php?uid=$1 [L]
In the htaccess file in your "s" folder, try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+s/profile\.php\?uid=([^&\ ]+)
RewriteRule ^ /s/%1.php? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)\.php$ /s/profile.php?uid=$1 [L]

Rewriting URL in CodeIgniter

I want to rewrite URL using .htaccess in codeigniter but it's not working. Can you please figure it out. Want to change URL from:
www.site.com/mauritius_holiday_rentals/apartment/anyname
to
www.site.com/mauritius_holiday_rentals/anyname
My current .htaccess file contains:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^mauritius_holiday_rentals/([A-Za-z0-9-]+)/?$ mauritius_holiday_rentals/apartment/$1 [L,QSA]
First four line is for removing index.php from URL which is working fine.
If routes file is set to access new url and you want to set redirection for old URLs then Use following code in .htaccess. otherwise let me know in detail what you want to do.
RewriteEngine on
RewriteBase /
RewriteRule /mauritius_holiday_rentals/apartment/$1 /mauritius_holiday_rentals/$1 [R=301,L]
Routes.php config file code
$route['mauritius_holiday_rentals/(:any)']="mauritius_holiday_rentals/apartment/$1";
Let me know if any problem.
RewriteEngine On
RewriteBase /yourProjectName/
RewriteCond $1 !^(images|stylesheets|javascript)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourProjectName/index.php?/$1 [L]
Try to use codeigniter routing system
https://www.codeigniter.com/user_guide/general/routing.html

Categories