简体   繁体   English

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

[英]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.

相关问题 像site.com/#/xyz一样,PHP / CodeIgniter是否可以读取URL? - Any way for PHP/CodeIgniter to read the URL, when it's like site.com/#/xyz …? 当我将我的浏览器指向网站site.com/index.php/"non-existent-dir“时显示index.php没有css的页面 - When i point my browser to website site.com/index.php/“non-existent-dir” shows index.php the page with no css mod_rewrite指向site.com/something到index.php?values = something - mod_rewrite to direct site.com/something to index.php?values=something 如何在web.config文件中重写URL以将index.php隐藏在类似http://demo.example.com/dir/dir/index.php/login的URL中? - How to Rewrite URL in web.config file to hide index.php in url like http://demo.example.com/dir/dir/index.php/login? 显示用户个人资料,例如site.com/user1 - Display user profile like site.com/user1 从这样的网址加载xml数据:/index.php/site/projects/作为php变量 - Load xml data from an url like this : /index.php/site/projects/ as a php variable .htaccess加载index.php文件以获取友好的URL - .htaccess to load index.php file for friendly url .htaccess将url.com/file.php?=$terms重定向到newurl.com/search.php?= $ terms(如果引荐来源网址不是site.com) - .htaccess redirect url.com/file.php?=$terms to newurl.com/search.php?=$terms if referrer is not site.com 将类似/ xyz的URL重定向到/index.php?goto=xyz - Redirecting URL like /xyz to /index.php?goto=xyz index.php应该不会像facebook一样显示在网址上? - index.php should not show up on the url like facebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM