Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以创建一个友好的url,比如site.com/state/127,并且仍然指向一个特定的文件,比如index.php_Php_Rest_.htaccess_Endpoint - Fatal编程技术网

有没有一种方法可以创建一个友好的url,比如site.com/state/127,并且仍然指向一个特定的文件,比如index.php

有没有一种方法可以创建一个友好的url,比如site.com/state/127,并且仍然指向一个特定的文件,比如index.php,php,rest,.htaccess,endpoint,Php,Rest,.htaccess,Endpoint,我正在创建一个RESTAPI端点以将数据输入数据库。我一直在尝试将url改写成这种格式http://example.com/src/public/endpoint/data。url中的public是一个文件夹,它有一个index.php文件,所有url都将路由到该文件,但该文件不起作用。下面是我的.htaccess代码 RewriteEngine RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d Re

我正在创建一个RESTAPI端点以将数据输入数据库。我一直在尝试将url改写成这种格式
http://example.com/src/public/endpoint/data
。url中的public是一个文件夹,它有一个index.php文件,所有url都将路由到该文件,但该文件不起作用。下面是我的.htaccess代码

RewriteEngine 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/src/public/index.php?path=$1 [NC,L,QSA]
我正在获取链接地址
http://example.com/src/public/?path=state/127&_=1579497796272
作为我期望的
example.com/src/public/state/127时的端点。我知道我做得不对,但我想不出来。

试试这个:

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]
首先,您应该在
RewriteEngine
之后添加
on
,这意味着启用mod_rewrite。 第二行将捕获请求并获取查询字符串的一部分。 第三行将从外部重定向请求,使其友好。 最后一行将在内部将新行重定向到正确的路径


注意:如果可以,将
R
更改为
R=301
以永久重定向。

什么不起作用?您正在键入的地址和提取的地址?一般来说,URL重写的想法是将“好的”URL(重写规则的第一部分)重写为“真实的”URL(第二部分),而不是相反。