简体   繁体   中英

Is there a way to create a friendly url like site.com/state/127 and still point to a particular file like index.php

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM