簡體   English   中英

如何修復 htaccess 文件中的 URL 解析、301 重定向

[英]how to fix URL Resolve, 301 redirects in the htaccess file

我是新手,我使用 PHP。 我的教授介紹了一個給我帶來網站問題的網站。 (woorank) 我的網站有 301 重定向問題。 看看下面的圖片。

點擊查看我的問題

我的問題是,我想在.htaccess文件中解決這個問題。 我知道下面的代碼回答了這個但錯了

// URL Resolve
if (currentUrl() == "http://example.com") {
    redirectToMainDomain();
} elseif (currentUrl() == "http://www.example.com") {
    redirectToMainDomain();
} elseif (currentUrl() == "https://example.com") {
    redirectToMainDomain();
} elseif (currentUrl() == "https://www.example.com") {
    redirectToMainDomain();
}

我在第一行代碼上寫了上面的代碼,但是我的問題沒有解決,我做了這樣的事情來應用到.htaccess文件中。

if (currentUrl() == "https://www.example.com" or currentUrl() == "https://example.com" or currentUrl() == "http://example.com" or currentUrl() == "http://www.example.com") {
        header("Location: https://example.com");
        exit;
    }
if (currentUrl() == "https://www.example.com/blog/index.php" or currentUrl() == "https://example.com/blog/index.php" or currentUrl() == "http://example.com/blog/index.php" or currentUrl() == "http://www.example.com/blog/index.php") {
        header("Location: https://example.com/blog/index.php");
        exit;
    } 
// and other pages

請指導

帶或不帶httpswww站點 URL 指向不同的位置,這就是您看到重定向錯誤的原因。 您可以使用 htaccess 文件中的以下代碼修復此問題:

RewriteEngine on

# rediret http and non-www to https://www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]

在上面的代碼中將example.com更改為您的域,並在清除瀏覽器緩存后進行測試。

RewriteEngine On
RewriteRule ^(.*)$ http://example2.com/ [R=301]

將此添加到位於站點文件夾中的.htaccess

我是問題的作者。 您可以使用.htaccess文件中的以下代碼修復此問題:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM