我一直在四處尋找,但似乎無法找到我的問題的確切答案。對于這個例子,我將使用domain.com作為我的域名。
我有 htaccess 規則來將漂亮的 url 重定向到 web 服務器中的實際頁面。像這樣
RewriteEngine on
RewriteBase /
...
RewriteCond %{QUERY_STRING} .
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING}
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1
如您所見,這基本上是在訪問domain.com/en/about-us url時加載/pages/about.php檔案。這就像它應該的那樣作業。
我的問題來自當有人試圖通過轉到domain.com/pages/about.php直接訪問 url 時,我不希望這種情況成為可能,或者通過基本上說同一頁面有 2 個 URL 來影響我的 SEO . 所以我通過做類似下面的代碼的事情來閱讀它。但它實際上只是保留輸入的 URL 而沒有做我需要的。
RewriteCond %{THE_REQUEST} \s/about\.php\?lang=en\s [NC]
RewriteRule ^ /en/about-us? [R=301,L]
RewriteRule ^en/about-us$ /pages/about.php?lang=en [L]
非常感謝任何幫助或澄清
編輯
我也在下面嘗試過這個,但它顯然只是一個無限回圈,將 lang 添加到查詢字串中。
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=en
RewriteRule ^ en/about-us [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=fr
RewriteRule ^ fr/a-propos [R=301,L]
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING} [L]
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1 [L]
uj5u.com熱心網友回復:
第一個解決方案:根據您顯示的嘗試/示例,請嘗試遵循 htaccess 規則檔案。在測驗您的 URL 之前,請確保清除瀏覽器快取。
RewriteEngine ON
##Direct request to url for about.php where query string has language as en.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=en [NC]
RewriteRule ^ en/about-us? [R=301,L]
RewriteRule ^(en)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Direct request to url for about.php where query string has language as fr.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=fr
RewriteRule ^ fr/a-propos? [R=301,L]
RewriteRule ^(fr)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Existing rules by OP with additonal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)/?$ pages/about.php?lang=$1&%{QUERY_STRING} [NC,L]
##Existing rules by OP with additonal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)/?$ pages/about.php?lang=$1 [NC,QSA,L]
第二種解決方案:或嘗試遵循使用THE_REQUEST變數設定的 htaccess 規則。確保使用上述或一次只遵循一個規則。
RewriteEngine ON
##Direct request to url for about.php where query string has language as en.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/pages/about\.php\?lang=en\s [NC]
RewriteRule ^ en/about-us? [R=301,L]
RewriteRule ^(en)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Direct request to url for about.php where query string has language as fr.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/pages/about\.php\?lang=fr\s [NC]
RewriteRule ^ fr/a-propos? [R=301,L]
RewriteRule ^(fr)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Existing rules by OP with addiotnal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING} [NC,L]
##Existing rules by OP with addiotnal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1 [NC,QSA,L]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357885.html
