抱歉,我真的不知道如何表達標題,但這一直困擾著我多年。我一直想知道有時在網站上如何看到奇怪的 url 結構。以優酷為例:
youtube.com/c/this_persons_channel
我只是不明白它是如何作業的!我知道 / 意味著它是網站內的一個檔案夾,但它是如何結束的。是檔案嗎?如果是這樣,檔案型別在哪里?這個檔案是由代碼制成的嗎?因為它是針對特定用戶的。當他們創建網站時,它不可能在那里。
如果這是一個愚蠢的問題,我很抱歉,但我不知道要搜索什么以了解更多資訊。
uj5u.com熱心網友回復:
我認為你在談論的是重寫網址。這可以在.htaccess您的專案檔案中完成。
url 的結尾不是檔案或檔案夾。它是一個查詢字串。可以通過代碼訪問并用于自定義頁面。獲取此問題的網址:
https://stackoverflow.com/questions/69684211/what-is-the-unusual-thing-that-i-sometimes-see-in-website-urls
也可以表示為:
https://stackoverflow.com/questions?id=69684211&name=what-is-the-unusual-thing-that-i-sometimes-see-in-website-urls
注意:這些可能不是查詢字串的實際名稱,只是一個示例。
通過寫作:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^index/([0-9] ) index.html?id=$1 [NC,L]
在.htaccess檔案中,你可以把url
http://localhost/index.php?id=5
進入
http://localhost/index/5
注意:這不會在您重繪 頁面時簡單地更改 url。但是如果我輸入'http://localhost/index/5',它會給我和我輸入'http://localhost/index.php?id=5'一樣的頁面。因此,您必須將所有重定向到該頁面的重定向更改為首選 url。所有這些代碼所做的就是使它不會將您發送到 404 錯誤頁面。如果您愿意,您可以撰寫一些 php 代碼,如果您在原始 url 上,這些代碼會自動重定向您。
網站這樣做有幾個原因:
- 使網址更容易記住,更容易寫,
- 使搜索引擎更容易找到該網站。
解釋 .htaccess 代碼
一旦你理解了代碼的含義,它實際上相當簡單。
第 1 行:
RewriteEngine on
通過將其設定為 on,它允許您重寫并為 url 制定自己的規則。
什么是重寫引擎?
第 2 行:
RewriteCond %{REQUEST_FILENAME} !-d
這一行是一個條件,有點像 if 陳述句,它表示如果請求的檔案名不是目錄,則繼續代碼。這是為了防止出現檔案與檔案夾同名的問題,并且可能會在 url 中造成混淆。
第 3 行:
RewriteCond %{REQUEST_FILENAME}\.php -f
這一行檢查請求的檔案是否具有擴展名“php”,它不必是 php,它可以是任何你想要的。
有關 .htaccess 規則和條件的更多資訊:.htaccess 重寫規則
第 4 行:
RewriteRule ^index/([0-9a-zA-Z_-] ) index.php?id=$1 [NC,L]
好的,這是最令人困惑的一行。它基本上描述了將 url 更改為什么的規則。
The '^' right at the beginning means ignore anything in the url before what comes after it. So it would ignore whatever comes before 'index' in the url.
Now you want to set the rules for what characters are allowed between index/ and the next '/' in the url. If you only have one query in the url then don't worry about adding another '/' at the end. But if you have lets say a ?name parameter, the url will be https://localhost/index/5/charlie for example. but because I have two search parameters, I have to add another regex. So the last line will now become RewriteRule ^index/([0-9a-zA-Z_-] )/([0-9a-zA-Z_-] ) index.php?id=$1 [NC,L]
In this example it allows all characters from 0-9, a-z (lowercase and capitals), and underscores and hyphens. So it will rewrite the url only if it contains those characters. If it contained a dollar sign $, for example, then it wouldn't rewrite.
Make sure you include the after the closing square bracket! Without the at the end, it would only allow 1 character values. So ?id=5 would work but ?id=52 wouldn't work.But with the there it will be fine.
Now, the next part of the line is telling the server what you want to redirect to in the url. In this case you want to redirect to index.php so you write that.
You also want to include the parameters so you can actually use them in your code. So now add on the query string/s. But we don't know what the ?id parameter is. So we need to put a placeholder there. So now type in ?id=$1. Make sure the variable is called 1 and not something else.
If you were to have more than one parameter, you would simply write ¶m2=$2 then ¶m3=$3 and so on.
The final thing you need to add is [NC,L]
Where the NC means Non Case which means if someone were to write index.php with a capital I, it would not matter. And where the L means the Rewrite Conditions from earlier only apply to that rule and not any rules you may have later on.
If you didn't understand my explanation watch Dani Krossing's video on it, he explains it very well: https://www.youtube.com/watch?v=zJxCq6D14eM
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/332957.html
上一篇:如何知道提供的URL是AndroidStudio中的影像還是視頻
下一篇:如何使用Go優雅地決議URI?
