[英]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.我正在创建一个 rest api 端点来将数据输入到数据库中。 I have been trying to rewrite the url into this format
http://example.com/src/public/endpoint/data
.我一直在尝试将 url 重写为这种格式
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. url 中的 public 是一个文件夹,其中包含一个 index.php 文件,所有 url 都将路由到该文件,但它不起作用。 Below is my .htaccess code.
下面是我的 .htaccess 代码。
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
.当我期望
example.com/src/public/state/127
时,我将链接地址http://example.com/src/public/?path=state/127&_=1579497796272
作为端点。 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 .首先你应该在
RewriteEngine
之后添加on
这意味着启用 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.注意:如果没问题,把
R
改成R=301
就可以永久重定向了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.