使用 301 状态代码将所有版本的 URL 重定向到主站点

使用 301 状态代码将所有版本的 URL 重定向到主站点

我想将以以下内容开头的所有请求永久重定向到https://www.example.com使用 301 代码。

http://example.com

http://www.example.com

https://example.com

我应该如何编辑我的 apache 配置文件?或者使用 .htaccess 文件?

我目前在我的 http 站点的 apache 配置中有这个:

<VirtualHost 5.x.x.x:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>

这是我的 https 配置中的内容:

<VirtualHost 5.x.x.x:443>
    ServerName example.com
    Redirect permanent / https://www.example.com/

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/example.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/example.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>

<VirtualHost 5.x.x.x:443>
    ServerName www.example.com
    ServerAdmin [email protected]
    DocumentRoot /home/example/public_html/public
    UseCanonicalName Off

    ...
    FULL CONFIG
    ...
</VirtualHost>

但是一个 Seo 测试网站(woorank)说 URL 解析到不同的地址(在前 3 个重定向的 URL 中,unicode 字符以小写编码,而在第 4 个未重定向的情况下,unicode 字符为大写?):

URL Resolve
Warning, no 301 redirects are in place to redirect traffic to your preferred example. Pages that load successfully both with and without www. are treated as duplicate content!
Not all versions of your page point to the same URL.

http://example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7 
to  
https://www.example.com/programming/253/%d8%ae%d8%b7%d8%a7%db%8c-java.lang.NullPointerException-%d8%af%d8%b1-%d8%ac%d8%a7%d9%88%d8%a7

http://www.example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7 
to  
https://www.example.com/programming/253/%d8%ae%d8%b7%d8%a7%db%8c-java.lang.NullPointerException-%d8%af%d8%b1-%d8%ac%d8%a7%d9%88%d8%a7

https://example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7
to
https://www.example.com/programming/253/%d8%ae%d8%b7%d8%a7%db%8c-java.lang.NullPointerException-%d8%af%d8%b1-%d8%ac%d8%a7%d9%88%d8%a7

https://www.example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7
to
https://www.example.com/programming/253/%D8%AE%D8%B7%D8%A7%DB%8C-java.lang.NullPointerException-%D8%AF%D8%B1-%D8%AC%D8%A7%D9%88%D8%A7

谢谢

答案1

你可以这样做

RewriteEngine On
RewriteRule ^/(.*)$ https://www.example.com/$1 [R=301,L]

相关内容