문제

I am helping out with a website that has a main/root website as well as a WordPress installation in a sub-directory. The .htaccess file of the main site has rules to redirect non-www traffic to 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. For instance:

  • site.com would redirect to www.site.com
  • site.com/some-content-belonging-to-root-site would redirect to www.site.com/some-content...
  • site.com/wordpress does not redirect to 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:

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.

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).

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.

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. The base wordpress URL is rewritten, but article URLs are not:

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

도움이 되었습니까?

해결책

It turned out that the problem was not the rewrite conditions or rules, but the order in which they appeared in the .htaccess file.

I read a WP forum post in which someone suggested moving the rewrites to the top of the file for performance reasons. 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. I got something like this:

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.

In case it happens to help anyone else, I began with a .htaccess file that looked like this:

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:

<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 WordPress website and I was looking for www to non www redirect for my website : fasterthemes.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. 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 :

i.stack.imgur.com/ie7f8.png

  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.fasterthemes.com , it redirects you to fasterthemes.com. Earlier it was throwing an error. I hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top