简体   繁体   English

通过HTTP GET请求传递的参数,而不是作为查询参数-slug / cleanurl / prettyurl

[英]parameter passing by HTTP GET request other than as query parameters- slug/cleanurl/prettyurl

Note: The URL, code used here is only for demonstration purpose of my example. 注意:此处使用的URL和代码仅用于示例说明。

I have seen that, For a HTTP GET request, if you want to pass a value for decision making, it is NOT passed through query String parameters for some good reasons. 我已经看到,对于HTTP GET请求,如果您想传递一个用于决策的值,出于某些原因,它不会通过查询String参数传递。

Lets' say there's a thumbnail image showing a bookstore in houston location, say " ABC Bookstore " The href attribute of that image is assumed as below 假设在houston位置有一个显示书店的缩略图,说“ ABC Bookstore ”,则假定该图片的href属性如下

domain.com/texas/abcbookstore-houston

This is what is needed, and the page shows the book store details, instead of the URL being shown as domain.com/texas/details.php?id=1 这是需要的,并且页面显示书店详细信息, 而不是将URL显示为domain.com/texas/details.php?id=1

Question: 题:

Any ideas how the URL is analysed to fetch the key? 任何想法如何分析URL以获取密钥? One website when I looked at Network tab of Chrome, it showed 当我查看Chrome的“网络”标签时,一个网站显示

Request Headers
:authority:www.domain.com
:method:GET
:path:/texas/abcbookstore-houston

My thoughts: 我的想法:

I can extract the last word after parsing the complete URL, and I get ' abcbookstore-houston ' 解析完整的URL后,我可以提取最后一个单词,然后得到“ abcbookstore-houston

Code I tried: 我试过的代码:

$url = "domain.com/texas/abcbookstore-houston";
$path = parse_url($url, PHP_URL_PATH);
//echo $path;//// prints "/texas/abcbookstore-houston"

$parts = explode('/', rtrim($path, '/'));
$id_str = array_pop($parts);
echo $id_str; // prints abcbookstore-houston

My thinking is that we can have one more column in the main bookstore table called ' nameofid ' and a query will fetch the ' id ' whose ' nameofid ' matches. 我的想法是,我们可以在bookstore主表中再增加一列名为“ nameofid ”的查询,查询将获取其“ nameofid ”匹配的“ id ”。 nameofid in this case is " abcbookstore-houston ". nameofid在这种情况下是“ abcbookstore-houston ”。

Summarized Question: Is this a correct approach? 总结的问题:这是正确的方法吗? I have seen that in many of the websites, they no longer pass the query parameters even if that's a GET request, instead the URL looks clean like in this use-case. 我已经看到,在许多网站中,即使这是一个GET请求,它们也不再传递查询参数,而是在此用例中,URL看起来很干净。

As @charlietfl mentioned, I was actually looking at Clean URLs also called Pretty URLs @charlietfl所述,我实际上是在查看Clean URL(也称为Pretty URL)

This is the actual book store details page 这是实际的书店详细信息页面

http://www.domain.com/texas/details.php?id=1

Basically this should be 基本上这应该是

http://www.domain.com/texas/details.php?id=abcbookstore-houston

I wanted this to be displayed in the address bar as 我希望它在地址栏中显示为

http://www.domain.com/texas/abcbookstore-houston

Finally, I found out the solution by making the below changes in .htaccess file 最后,我通过对.htaccess文件进行以下更改来找到解决方案

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:texas/)?([^/]+)/?$ details.php?id=$1 [NC,L,QSA]

The final command can be replaced by 最终命令可以替换为

RewriteRule ^texas/([^/]+)/?$ details.php?id=$1 [NC,L,QSA]

if ' texas ' is mandatory and not optional . 如果' texas '是强制性的而不是可选的

So what is actually happening here is when I search for this URL http://www.domain.com/texas/abcbookstore-houston , the server is actually routing to http://www.domain.com/texas/details.php?id=abcbookstore-houston while we see only http://www.domain.com/texas/abcbookstore-houston in the address bar. 因此,实际上发生的是当我搜索该URL http://www.domain.com/texas/abcbookstore-houston ,服务器实际上正在路由到http://www.domain.com/texas/details.php?id=abcbookstore-houston而我们在地址栏中仅看到http://www.domain.com/texas/abcbookstore-houston

So inside details.php we can get the id using $_GET["id"] and continue our business logic. 因此,在details.php内部,我们可以使用$_GET["id"]获取id并继续我们的业务逻辑。


Additional Notes: 补充笔记:

If the objective was http://www.domain.com/abcbookstore-houston then the RewriteRule would be 如果目标是http://www.domain.com/abcbookstore-houston则RewriteRule将是

RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA]

Here's more explanation about the command used 这是有关所用命令的更多说明

  • RewriteEngine On turns the engine on. RewriteEngine On打开引擎。

  • RewriteCond %{REQUEST_FILENAME} !-f does not rewrite anything if the request filename exists, and is a file. RewriteCond %{REQUEST_FILENAME} !-f如果请求文件名存在且为文件,则不会重写任何内容。

  • RewriteCond %{REQUEST_FILENAME} !-d does not rewrite anything if the request filename exists, and is a directory. RewriteCond %{REQUEST_FILENAME} !-d如果请求文件名存在并且是目录,则不重写任何内容。

  • RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA] This is the actual rewrite rule. RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA]这是实际的重写规则。 It takes anything after the domain name (anything other than forward slashes), and rewrites it to details.php , passing it as the id parameter. 它需要域名之后的任何内容(除正斜杠之外的任何内容),并将其重写为details.php ,并将其作为id参数传递。

  • RewriteRule ^texas/([^/]+)/?$ details.php?id=$1 [NC,L,QSA] This is the actual rewrite rule. RewriteRule ^texas/([^/]+)/?$ details.php?id=$1 [NC,L,QSA]这是实际的重写规则。 It takes anything after {the domain name followed by the string ' texas '} (anything other than forward slashes), and rewrites it to details.php , passing it as the id parameter. 它需要{域名后跟字符串' texas '}之后的任何内容(除正斜杠之外的任何内容),并将其重写为details.php ,并将其作为id参数传递。

Note: 注意:

The technical term for word used in this use-case abcbookstore-houston is slug https://en.wikipedia.org/wiki/Semantic_URL#Slug 该用例abcbookstore-houston使用的单词的技术术语是slug https://en.wikipedia.org/wiki/Semantic_URL#Slug

A slug is the part of a URL which identifies a page using human-readable keywords. slug是URL的一部分,它使用人类可读的关键字标识页面。

To make the URL easier for users to type, special characters are often removed or replaced as well. 为了使URL易于用户输入,通常也删除或替换特殊字符。 For instance, accented characters are usually replaced by letters from the English alphabet; 例如,带重音的字符通常被英语字母中的字母代替; punctuation marks are generally removed; 标点符号通常会被删除; and spaces (which have to be encoded as %20 or +) are replaced by dashes (-) or underscores (_), which are more aesthetically pleasing. 和空格(必须将其编码为%20或+)替换为破折号(-)或下划线(_),从美学上讲更令人愉悦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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