简体   繁体   English

如何在 GCP 中的 Bitnami 认证的 wordpress 中配置 www 到非 www 重定向?

[英]How to configure www to non-www redirect in Bitnami certified wordpress in GCP?

I have my site running in GCP, I need to edit the .htaccess file to create a redirect that redirects www to non www.我的网站在 GCP 中运行,我需要编辑 .htaccess 文件以创建将 www 重定向到非 www 的重定向。 How do I configure it?.我如何配置它?。 I don't see .htaccess file in my website, I used a file manager plugin to access wordpress core files.我在我的网站上没有看到 .htaccess 文件,我使用文件管理器插件来访问 wordpress 核心文件。

I read somewhere bitnami has htaccess configured differently,.How do I create the redirect?我在某处读到 bitnami 的 htaccess 配置不同,如何创建重定向?

BTW, my httpd-vhosts.conf file has the following lines of code,.顺便说一句,我的 httpd-vhosts.conf 文件有以下几行代码。

<VirtualHost *:80>
    ServerName wordpress.example.com
    ServerAlias www.wordpress.example.com
    DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"

    Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

<VirtualHost *:443>
    ServerName wordpress.example.com
    ServerAlias www.wordpress.example.com
    DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
    SSLEngine on
    SSLCertificateFile "/opt/bitnami/apps/wordpress/conf/certs/server.crt"
    SSLCertificateKeyFile "/opt/bitnami/apps/wordpress/conf/certs/server.key"

    Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

It literally has example.com, not mydomain.com, should I be worried?它确实有example.com,而不是mydomain.com,我应该担心吗?

There are many ways to implement redirection.有很多方法可以实现重定向。 A simple method is to modify your apache conf file.一个简单的方法是修改你的 apache conf 文件。 Replace example.com with your domain name.将 example.com 替换为您的域名。

<VirtualHost *:80>
    ServerName example.com
    # Redirect everything arriving on HTTP to HTTPS
    Redirect "/" "https://example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    # Redirect anything that is not "example.com"
    <If "%{HTTP_HOST} != 'example.com'">
        Redirect "/" "https://example.com/"
    </If>
</VirtualHost>

Redirecting and Remapping with mod_rewrite使用 mod_rewrite 重定向和重新映射

Read this link for common use cases to avoid:阅读此链接了解常见用例以避免:

When not to use mod_rewrite何时不使用 mod_rewrite

You should modify ServerName to represent your server's domain name.您应该修改ServerName以表示您服务器的域名。 This should match what you configure in WordPress Settings for WordPress Address and Site Address .这应该与您在 WordPress 设置中为WordPress AddressSite Address配置的内容相匹配。

If you are using bitnami wordpress that you could configure directly in wordpress cause the only .htaccess that has wordpress is in如果您使用的是可以直接在 wordpress 中配置的 bitnami wordpress,则会导致唯一具有 wordpress 的 .htaccess 位于

/opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/akismet/.htaccess /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/akismet/.htaccess

I suggest you change the name site, here is the link of wordpress to do it.我建议您更改名称站点,这里是 wordpress 的链接 Also consider in your dns records have the records还要考虑在您的 dns 记录中有记录

wordpressexample.com A external IP www.wordpressexample.com CNAME wordpressexample.com wordpressexample.com 外部 IP www.wordpressexample.com CNAME wordpressexample.com

This is in order to enable me to get access to your website from both links.这是为了使我能够从两个链接访问您的网站。

Since you're not asking about why my WordPress website not showing , I assume that your WordPress website just working fine, so you can let the ServerName as is.由于您没有问why my WordPress website not showing ,我假设您的 WordPress 网站运行良好,因此您可以保持 ServerName 不变。

Furthermore, refer to the document of Apache HTTP Server此外,请参阅Apache HTTP Server文档

Due to the fact that the virtual host with ServerName www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server.由于ServerName为www.example.com的虚拟主机在配置文件中排在第一位,因此具有最高优先级,可以看作是默认或主服务器。

That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first .这意味着如果收到的请求与指定的 ServerName 指令之一不匹配,它将由 this first 提供服务。

Even though the ServerName might not match your real domain, the first VirtualHost on *:80 will be used.即使 ServerName 可能与您的真实域不匹配,也将使用*:80 上的第一个 VirtualHost

So if your WordPress website is working, I'll recommend that just keep it example.com.因此,如果您的 WordPress 网站正常运行,我建议您保留 example.com。

For the redirect setting, refer to the article on Bitnami重定向设置参考Bitnami文章

You can try to use SSH and use one of text editors edit the /opt/bitnami/apps/wordpress/conf/httpd-vhosts.conf file:您可以尝试使用SSH并使用文本编辑器之一编辑/opt/bitnami/apps/wordpress/conf/httpd-vhosts.conf文件:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
    ServerName wordpress.example.com
    ServerAlias www.wordpress.example.com
    DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"

    Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

<VirtualHost *:443>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
    ServerName wordpress.example.com
    ServerAlias www.wordpress.example.com
    DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
    SSLEngine on
    SSLCertificateFile "/opt/bitnami/apps/wordpress/conf/certs/server.crt"
    SSLCertificateKeyFile "/opt/bitnami/apps/wordpress/conf/certs/server.key"

    Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

You will need to restart the Apache HTTP Web Server in order to reload the setting file.您需要重新启动 Apache HTTP Web 服务器才能重新加载设置文件。

$sudo /opt/bitnami/ctlscript.sh restart apache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM