站點崩潰后,重定向 php 腳本無法按預期作業。我們試圖修復它,但與此同時,我們正在尋找一種快速的解決方案來重定向搜索引擎結果,以便我們的訪問者至少可以在點擊相關網頁后訪問。
url 結構或搜索引擎結果是這樣的:
https://www.example.com/MainCategory/SubCategory_1/SubCategory_2/Product?page=1
我想使用 URL 的“SubCategory_2”部分重定向到這樣的東西
https://www.example.com/SubCategory_2.php
所以在我們完全修復腳本之前,至少我們的訪問者會看到一個相關的網頁。
我很困惑......有什么想法嗎?謝謝
uj5u.com熱心網友回復:
要重定向指定的 URL,其中所有部分都是可變的(包括完全可變的,但存在查詢字串),那么您可以使用根.htaccess檔案頂部附近的 mod_rewrite 執行以下操作(或者至關重要的是,在任何現有的內部重寫之前):
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^[^/] /[^/] /([^/] )/[^/] $ /$1.php [QSD,R=302,L]
該QSD標志是從重定向回應中丟棄原始查詢字串所必需的。
以上將重定向:
/MainCategory/SubCategory_1/SubCategory_2/Product?page=1到/SubCategory_2.php/foo/bar/baz/qux?something到/baz.php
您可以使用此 htaccess 測驗器在此處對其進行測驗。
更新:
不幸的是沒有成功。我收到 404 錯誤。
/SubCategory_2.php如果指令與請求的 URL 不匹配或不存在,您將收到 404 。
URL 是否重定向?您在瀏覽器的地址欄中看到了什么?
如果沒有重定向,則上述規則與請求的 URL 不匹配,并且該規則什么也不做。要么是因為:
- URL格式與問題中所述不同。
- 規則在
.htaccess檔案中的錯誤位置。如前所述,此規則需要靠近組態檔的頂部。
我在這里找到了一個基本的解決方案 htaccess 重定向如果 URL 包含某個字串我創建這樣的東西
RewriteRule ^(.*)SubCategory_2(.*)$ https://example.com/SubCategory_2.php[L,R=301]并且作業得很好。我的問題是這是一個“靜態解決方案”,因為“SubCategory_2”是一個變數。
Ok, but that is a very generic (arguably "too generic") solution for the problem you appear to be attempting to solve. This matches "SubCategory_2" anywhere in the URL-path (not just whole path segments) and preserves any query string (present on your example URL) through the redirect. So, this would not perform the stated redirect on the example URL in your question.
However, the directive you've posted (which you say "works just fine") cannot possibly work as written, at least not by itself. Ignoring the missing space (a typo I assume) before the flags argument, this would result in an endless redirect loop, since the target URL /SubCategory_2.php also matches the regex ^(.*)SubCategory_2(.*)$.
Also, should this be a 301 (permanent) redirect? You seem to imply this is a "temporary" solution?
HOWEVER, it's not technically possible to make "SubCategory_2" entirely variable in this "basic solution" and search for this variable "something" in a larger string and redirect to "something.php". How do you know that you have found the correct part of a much larger URL? You need to be more specific about what you are searching for.
In your original question you are extracting the 3rd path segment in a URL-path that consists of 4 path segments and a query string. That is a perfectly reasonable pattern, but you can't extract "something" when you don't know what or where "something" is.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/423066.html
標籤:
