.htaccess redirects
-
I've done some research but can't find a good answer to this question. Here's my situation:
Site redirects from example.com to www.example.com just fine. However, it doesn't work so well for internal pages.
My site incorrectly redirects (non-www) example.com/page2 to www.example.com when it should instead go to www.example.com/page2
So I need a method to redirect non-www internal pages to www versions. Currently I have this in my .htaccess - do I need to modify the rules?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]Thanks
-
The php declaration is to force your server to use php version 5.3, 3Plains. It's often put in place when a site's applications require a more recent version of php than is the default on the server.
@Aleya - his htaccess had the php declaration in the middle of some of his conditionals, which I suspect was the issue. Had him move the php declaration to the top of the file before turning rewrite engine on. Seems to have resolved the issue.
(Note, the php declaration can also be placed as last line in the file. I just find it better at the top so it reminds it's there in case I have a php version issue after a future server upgrade)
Pleased we got ya working
Paul
-
Paul figured it out. You the man!
-
Hey Aleyda,
Thanks for the help, but unfortunately it still won't work. There are 2 other pieces of info in my .htaccess file.
1. Some PHP stuff (not sure what it does)
Use PHP 5.3
AddType application/x-httpd-php53 .php
2. 301 redirects I did a few days ago (about 30 articles that looks like this)
Redirect 301 /article http://www.example.com/blog/article
Redirect 301 /article2 http://www.example.com/blog/article2
....It must be one of these two remaining issues...?
-
Hi again. I've just seen your answer above, that you also have the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]In your htaccess so I've added it before too:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]and again worked fine! Do you have something additional to that?
Thanks!
-
It's there something else then in your htaccess? I also checked it with http://htaccess.madewithlove.be/ and worked fine. Could you please test it with another domain so you can take a look it's ok? I'm afraid that it might be something else that you have configured there and that's why it doesn't work since the code is alright.
-
Tried it,
Same results as above.
-
Hi Ryan,
For non-www to www redirects (for all the URLs under the domain) you can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]Thanks!
-
Sent you an email.
-
i'd try disabling all plugins and test. Also ensure in General >> Settings the URLs are including the www (I assume this is Wordpress). There seems to be some internal configuration causing this.
-
Ryan, it seems there must be a conflict in your htaccess file? If you're willing, you can PM me a copy of the full file and the site URL and I'll see if i can find anything.
Paul
-
Tried it, and same result as before.
I've even tried this from SEOmoz:
RewriteCond %{HTTP_HOST} !^www.seomoz.org [NC]
RewriteRule (.*) http://www.seomoz.org/$1 [L,R=301]....and it gave my server an error. I don't know I guess something funky is going on.
-
That last rule is the Wordpress default rule slightly trimmed:
<ifmodule mod_rewrite.c="">RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]</ifmodule>I would try this version and revert if no good. I'd also install Fiddler and watch all the header responses to see if there is a clue.
-
Thanks for the tip Paul, but this method isn't helping me out. I still get:
example.com/page2 redirecting to www.example.com
The redirect result you've given me is what I've gotten from a few other methods as well. Perhaps there is something else at play. In my .htaccess file I have another Rewrite going on before the non www redirect we're discussing now. It is this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]......do you think this could be messing something up for me?
-
Replace you RewriteCond and RewriteRule with these 2 lines of code, Ryan:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]Paul
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
301 redirect from sites closing down
Hi We have around 10 supplementary sites that have links to our site which are now closing down but are out of our control. We could have access to their domains so how could we maintain the link juice from these old sites which are going to our new site? However there will be no websites left on these old supplementary just domain names
Technical SEO | | ocelot0 -
60% Internal Redirects
A new client I am working with has a site with over 9,000 internal 301 redirects. These are as a result of old links not being updated and the number of internal 301 redirects far outweighs the number of 'correct' links on the site. My personal opinion is that creates the risk of crawl errors/issues and whilst a 301 redirect is correct in this case, it does not negate the need to update internal links. The problem I have is that when I explain this to the client, they reply with an Matt Cutts video from 2008 that talks about 301 redirects being correct for site migrations. Even though the video is not entirely relevant to the point, I can not get the client to move from his position. Ideally, what I am looking for help with is the following: Am I right in my position that having this many redirects is a potential issue and that internal links should be updated? Does anyone know of any articles from 'notable/reputable' sources that I can use in order to support my position? Thanks in advance for your help.
Technical SEO | | MattHopkins0 -
Htaccess code to 301 redirect a folder change
Hi, I need some help to redirect all my site as there was a folder change. eg, the old structure was www.mysite.com/stuff-1/bags.html and I need it to go to the same structure without the "-1" eg: /stuff/bags.html
Technical SEO | | Paul_MC
The "bags.html" will be lots of different products, so this would be a wildcard? What would the htaccess code need to be? Thanks0 -
301 redirect from Blogger
Hello, I have a client with a Wordpress network of blogs, each blog is owned by a different blogger. Many of them were migrated time ago from Blogger. I have seen that the way used to redirect them is a meta refresh, so no authority is being passed. I cannot find any reliable way of making a 301 from Blogger, There are some plugins, but I'm afraid of using them. Any of you have experience with this situation please? I have even thought about placing a global rel canonical before the meta refresh, but I think that here the problem is the meta refresh itself.... Thank you in advance
Technical SEO | | Juandbbam0 -
Questions about Redirects
Hi, I am trying to make sure that I can determine if a site has a 301 redirect set up to redirect the site from domain.com to www.domain.com and am hoping that you can confirm the following for me, or let me know if I am off track: is http://www.internetofficer.com/seo-tool/redirect-check/ a reliable way to check if a 301 redirect is set up? is Screaming Frog SEO Spider a good tool to use to see if a redirect is in place? if I search for site:www.domain.com and site:domain.com, I should only get results for the site being indexed, not for the site that has the 301 redirect set up, right? For example, if www.domain.com is set up to redirect to domain.com, then I should get no search results for site:www.domain.com and only show indexed pages for domain.com. If I search for site:www.domain.com and site:domain.com and get results for both, then does this mean that the redirect is not set up? if a redirect is set up from www.domain.com to domain.com, should the crawl report should only show one page crawled on www.domain.com? if a crawl report shows same number of pages for www.domain.com as for domain.com, does that mean that redirect is not set up properly? Thanks in advance for your help! Carolina
Technical SEO | | csmm0 -
Should I change a 301 redirect?
I recently moved all the content from an old site to a new site on a new domain. I lost a significant amount of traffic as a result. There are 301 redirects for every page on the old site. Generally, these point to the same content as was on the relevant page of the old site. However, the 301 redirect for the homepage on the old site points to the homepage on the new site, not to the content from the old site homepage. I'm wondering whether to change the 301 to point at the content from the old site homepage. Any advice would be much appreciated.
Technical SEO | | seqal0 -
Is my 301 redirect working?
Very simple question here . I've redirected a bunch of older pages with decent ranking to some newer pages on my site, using the Thesis theme's built-in redirect function. However, in the SERPS, the older pages (and, importantly, older titles) still show up. When clicked on, they redirect to the new page, but it's still irritating because the older titles make the site look out of date. Is this Working As Intended, or have I or my theme done something wrong? And if it's the latter, what's the best way to achieve a redirect, preferably with a Wordpress plugin?
Technical SEO | | Cairmen0 -
301 redirect: Old pages to new
Hi, We have recently made some changes to our agency site. Looking in webmaster tools we have identified a number of old pages with existing link juice. Not a great deal mostly 32/100 PA. There are a mixture of URLs "meet the team" and people pages etc. The anchor text on the majority of pages is our brand name. Could we now 301 all these pages to one page, or is this a no no in the eyes of Google? Any help greatly appreciated. Best Regards Sean
Technical SEO | | Yozzer0