0

I am creating a rest api endpoint to input data into a database. I have been trying to rewrite the url into this format http://example.com/src/public/endpoint/data. The public in the url is a folder that had has an index.php file that all the urls will be routed to but it is not working. Below is my .htaccess code.

RewriteEngine 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/src/public/index.php?path=$1 [NC,L,QSA]

I am getting the link address http://example.com/src/public/?path=state/127&_=1579497796272 as the endpoint when i expect example.com/src/public/state/127. I know am not doing something correctly but i can not figure it out.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • What isn't working? What address are you typing and what address appears to be fetched? The idea of URL rewriting in general is you're rewriting the "nice" URL (first part of `RewriteRule`) into the "real" one (second part), not the other way around. – Jeto Jan 20 '20 at 05:51

1 Answers1

0

Try this :

RewriteEngine On
RewriteCond %{THE_REQUEST} \?path=(.*)&(.*)\sHTTP.*$
RewriteRule ^(.*)$ /src/public/%1? [L,R]
RewriteRule ^src/public/state/127 /src/public/?path=state/127&_=1579497796272 [L]

First you should add on after RewriteEngine which means enabling mod_rewrite . The second line will captrue the request and get a part of query string. Third line will externally redirect the request to be friendly. The last line will redirect a new one, internally , to the correct path.

Note: if it is ok , change R to R=301 to be permenant redirection.

Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18