使用 htaccess 永久 (301) 将所有 url 重定向到主页

时间:2021-02-23 14:22:57

标签: wordpress apache .htaccess redirect

我有一个有很多页面的网站,我希望所有页面/网址都将被 htaccess 永久重定向(301)到主页。我们可以让我如何实现这一目标吗?

我想要这样的东西。

mywebsite.comhttps://www.mywebsite.com/

mywebsite.com/pagehttps://www.mywebsite.com/

mywebsite.com/assets/img.jpghttps://www.mywebsite.com/

这是我当前的 htaccess 行

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

谢谢

1 个答案:

答案 0 :(得分:1)

将我的评论转换为答案,以便将来的访问者轻松找到解决方案。

您可以使用这个单一规则:

RewriteEngine On

# redirect to landing page if it is
# non-www or non-https or any URL other than landing page
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule . https://www.%1/ [R=301,L]
相关问题