
基本上,我想要這個網址:
http://domain.tld/auth.php?state=somestate&token=somelongtoken
重寫/重新導向為:
http://domain.tld/auth/validate/somestate/somelongtoken
我已經在網上查了很長一段時間了,但我不知道如何做這件事..
另外 - 重定向或重寫是最好的嗎?
除了透過 API 驗證使用者之外,我不會將該 URL 用於任何其他用途,因此它只是在訪問該頁面後重定向使用者。
答案1
有一些未經測試的程式碼...
# Make sure you are accessing auth.php
RewriteCond %{REQUEST_URI} ^/auth.php$
# Check that state and token are present (in that order...)
RewriteCond %{QUERY_STRING} state=[^&]*token=
# Rewrite the URI
RewriteRule ^/auth.php\?state=\([^&]*\)\&token=\([^&]*\)$ auth/validate/\1/\2 [L,QSA]
我會使用重寫,因為你說 /auth.php 已經執行了 Location: 操作。發送兩個重定向確實沒有意義。另外,這對搜尋引擎來說是不好的做法(我知道在這種情況下它不會發生,只是提一下,因為如果這對SEO 不利,那麼對您的用戶來說可能就不那麼好了。
請注意,RewriteRule 和前面的條件的正規表示式期望這兩個查詢字串參數按該順序排列。您需要多行才能不強制執行訂單。但我想這不是問題。