简体   繁体   English

非www到www重定向不适用于WordPress子目录安装中的文章

[英]non-www to www redirect not working for articles in subdirectory installation of WordPress

I am helping out with a website that has a main/root website as well as a WordPress installation in a sub-directory. 我正在帮助一个具有主/根网站以及子目录中的WordPress安装的网站。 The .htaccess file of the main site has rules to redirect non-www traffic to www: 主站点的.htaccess文件具有将非www流量重定向到www的规则:

RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule (.*) http://www\.site\.com/$1 [R=301,L]

However, this has no effect on the WP site. 但是,这对WP网站没有影响。 For instance: 例如:

  • site.com would redirect to www.site.com site.com将重定向到www.site.com
  • site.com/some-content-belonging-to-root-site would redirect to www.site.com/some-content... site.com/some-content-belonging-to-root-site将重定向到www.site.com/some-content ...
  • site.com/wordpress does not redirect to www.site.com/wordpress site.com/wordpress不会重定向到www.site.com/wordpress

I added similar rules to the WordPress .htaccess, playing around to try to get the result I would expect, but the best I could come up with is this: 我在WordPress .htaccess中添加了类似的规则,尝试获得我期望的结果,但是我能想到的最好的方法是:

RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]

...which successfully redirects site.com/wordpress to www.site.com/wordpress, but does not redirect site.com/wordpress/a-blog-post. ...成功将site.com/wordpress重定向到www.site.com/wordpress,但没有将site.com/wordpress/a-blog-post重定向。

I feel like I must be missing something simple or obvious, but hours of experimentation and research have not yielded a thing except that .htaccess files in sub-directories are not advised (but I think this is unavoidable in this case). 我觉得我肯定缺少一些简单或明显的东西,但是经过数小时的实验和研究并没有产生任何结果,只是不建议在子目录中使用.htaccess文件(但在这种情况下,我认为这是不可避免的)。

I would be grateful for any suggestions on how to fix the rewrite condition or rule to successfully redirect the post links of the WP site. 对于如何解决重写条件或规则以成功重定向WP网站的帖子链接的任何建议,我将不胜感激。

Thanks! 谢谢!

EDIT / ADDITIONAL INFO: I modified the RewriteCond to better match the incoming URLs, thinking that this might help, but it has the same effect. 编辑/其他信息:我修改了RewriteCond以更好地匹配传入的URL,认为这样做可能有所帮助,但是效果相同。 The base wordpress URL is rewritten, but article URLs are not: 基本的WordPress URL已被重写,但文章URL却没有:

RewriteCond %{HTTP_HOST}/wordpress/.* ^site\.com/wordpress/.* [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]

I wouldn't think that it should matter, but just in case, I'll mention that the article URLs look like this: site.com/wordpress/2014/05/16/article-slug 我认为这无关紧要,但以防万一,我会提到文章URL如下所示:site.com/wordpress/2014/05/16/article-slug

It turned out that the problem was not the rewrite conditions or rules, but the order in which they appeared in the .htaccess file. 事实证明,问题不在于重写条件或规则,而是它们在.htaccess文件中出现的顺序

I read a WP forum post in which someone suggested moving the rewrites to the top of the file for performance reasons. 我读了一个WP论坛帖子,其中有人出于性能原因建议将重写内容移到文件顶部。 I gave it a shot just to see what would happen and noticed that the URL for the articles was affected - not in a good way, but affected nonetheless. 我试了一下只是为了看看会发生什么,并注意到文章的URL受到了影响-并不是很好,但是仍然受到了影响。 I got something like this: 我得到这样的东西:

www.site.com/wordpressindex.php?/2014/05/16/article-slug www.site.com/wordpressindex.php?/2014/05/16/article-slug

There were some rewrite rules to rewrite the base and redirect to the index given a nonexistent directory or file, which was what was building the garbled URL above. 给定一个不存在的目录或文件,存在一些重写规则来重写基础并重定向到索引,这正是上面构建乱码的URL。

In case it happens to help anyone else, I began with a .htaccess file that looked like this: 万一碰巧对其他人有帮助,我从一个看起来像这样的.htaccess文件开始:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress

And ended up rearranging it to look like this, which now works to properly redirect all blog-related URLs to the www site: 并最终将其重新排列为如下所示,现在可以正确地将所有与博客相关的URL重定向到www站点:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /wordpress/

    RewriteCond %{HTTP_HOST}/wordpress ^site\.com/wordpress [NC]
    RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]

    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

    RewriteRule ^index\.php$ - [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wordpress/index.php [L]
</IfModule>

Try: 尝试:

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]

And clean your browser history to make sure. 并确认您的浏览器历史记录。

I also own a WP website and I was looking for www to non www redirect for my website cnaclassestests.com 我还拥有一个WP网站,并且我一直在寻找www到非www重定向的网站cnaclassestests.com

The website is hosted in DigitalOcean and after struggling with several DAYS, I could figure out that all the solutions (.htaccess) codes available work fine if you've set the A record in your DNS settings properly. 该网站托管在DigitalOcean中,经过几天的努力,我发现如果您在DNS设置中正确设置了A记录,那么所有可用的解决方案(.htaccess)代码都可以正常工作。 If it's not set then the redirection doesn't work ! 如果未设置,那么重定向将不起作用!

So in my case, 所以就我而言

  1. I created one A record in my Digitalocean DNS like below : 我在Digitalocean DNS中创建了一个A记录,如下所示:

  1. I added following code : 我添加了以下代码:

     RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

And now as you can see if you browse http://www.cnaclassestests.com , it redirects you to cnaclassestests.com. 现在,您可以查看是否浏览了http://www.cnaclassestestss.com ,它会将您重定向到cnaclassestests.com。 Earlier it was throwing an error. 之前它抛出了一个错误。 I hope this helps. 我希望这有帮助。

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

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