如何修复 URL 解决,htaccess 文件中的 301 重定向
how to fix URL Resolve, 301 redirects in the htaccess file
我是新手,我工作PHP。我的教授介绍了一个给我网站问题的网站。 (伍兰克)
我的站点存在 301 重定向问题。看下图。
Click to see my problem
我的问题是,我想在 .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
请指导
RewriteEngine On
RewriteRule ^(.*)$ http://example2.com/ [R=301]
将此添加到 .htaccess
,它位于您站点的 root 文件夹中。
带或不带 https
和 www
的站点 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
更改为您的域,并在清除浏览器缓存后进行测试。
我是问题的作者
您可以在 .htaccess
文件中使用以下代码解决此问题:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
我是新手,我工作PHP。我的教授介绍了一个给我网站问题的网站。 (伍兰克) 我的站点存在 301 重定向问题。看下图。
Click to see my problem
我的问题是,我想在 .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
请指导
RewriteEngine On
RewriteRule ^(.*)$ http://example2.com/ [R=301]
将此添加到 .htaccess
,它位于您站点的 root 文件夹中。
带或不带 https
和 www
的站点 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
更改为您的域,并在清除浏览器缓存后进行测试。
我是问题的作者
您可以在 .htaccess
文件中使用以下代码解决此问题:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]