Note: The URL, code used here is only for demonstration purpose of my example.
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.
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
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
Question:
Any ideas how the URL is analysed to fetch the key? One website when I looked at Network tab of Chrome, it showed
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
'
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. nameofid
in this case is " 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.
As @charlietfl mentioned, I was actually looking at Clean URLs also called Pretty URLs
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
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 .
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.
So inside details.php
we can get the id
using $_GET["id"]
and continue our business logic.
Additional Notes:
If the objective was http://www.domain.com/abcbookstore-houston
then the RewriteRule would be
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA]
Here's more explanation about the command used
RewriteEngine On
turns the engine on.
RewriteCond %{REQUEST_FILENAME} !-f
does not rewrite anything if the request filename exists, and is a file.
RewriteCond %{REQUEST_FILENAME} !-d
does not rewrite anything if the request filename exists, and is a directory.
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA]
This is the actual rewrite rule. It takes anything after the domain name (anything other than forward slashes), and rewrites it to details.php
, passing it as the id
parameter.
RewriteRule ^texas/([^/]+)/?$ details.php?id=$1 [NC,L,QSA]
This is the actual rewrite rule. 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.
Note:
The technical term for word used in this use-case abcbookstore-houston
is 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.
To make the URL easier for users to type, special characters are often removed or replaced as well. 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.
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.