.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
-
Redirects Advice Please
Hi All, I have been approached by someone to look at their website who has seen a rank drop over the last week of around 15 places. On a quick look at their website I have seen what I am imaging could be the culprit as I imagine it will be creating a re-direct loop. However, i am not 100% with these things so would like some others opinions.com They have a wordpress website. There home page lets say https://theirsite.com/ They have an internal page built for a search term https://www.theirsite.com/keyword In wordpress they have set that page in settings to be the homepage. However, I looked on their server and via htaccess they have a 301 redirect from https://www.theirsite.com/keyword to https://www.theirsite.com/ So the questions are: 1. Could this be creating a loop? 2. The redirect was placed around a week before the rank drop. Could this possibly be the cause of the drop? 3. I am assuming that removing the 301 from htaccess is recommended? Thanks in advance for any advice
Technical SEO | | DaleZon0 -
Redirect Chain Domain
MozPro is highlighting some redirect chain issues with our domain that I do not recall ever setting up in our redirect list. In our Moz Pro Campaign I see the Site Crawl has flagged 36 Redirect Chain Issues. I understand how the redirect chain errors can happen but I do not recall ever manually redirecting our domain, yet I have http://stickylife.com, https://stickylife.com & https://www.stickylife.com all associated in one of our redirect chain errors. When looking at our redirect files I do not see any of these domain redirects and wonder how this has happened and how to fix it. It appears as though our HTTP and HTTPS is causing some redirection. I wonder if this is coming from our DNS settings?
Technical SEO | | StickyLife0 -
What's with the redirects?
Hi there,
Technical SEO | | HeadStud
I have a strange issue where pages are redirecting to the homepage.Let me explain - my website is http://thedj.com.au Now when I type in www.thedj.com.au/payments it redirects to https://thedj.com.au (even though it should be going to the page https://thedj.com.au/payments). Any idea why this is and how to fix? My htaccess file is below: BEGIN HTTPS Redirection Plugin <ifmodule mod_rewrite.c="">RewriteEngine On
RewriteRule ^home.htm$ https://thedj.com.au/ [R=301,L]
RewriteRule ^photos.htm$ http://photos.thedj.com.au/ [R=301,L]
RewriteRule ^contacts.htm$ https://thedj.com.au/contact-us/ [R=301,L]
RewriteRule ^booking.htm$ https://thedj.com.au/book-dj/ [R=301,L]
RewriteRule ^downloads.htm$ https://thedj.com.au/downloads/ [R=301,L]
RewriteRule ^payonline.htm$ https://thedj.com.au/payments/ [R=301,L]
RewriteRule ^price.htm$ https://thedj.com.au/pricing/ [R=301,L]
RewriteRule ^questions.htm$ https://thedj.com.au/faq/ [R=301,L]
RewriteRule ^links.htm$ https://thedj.com.au/links/ [R=301,L]
RewriteRule ^thankyous/index.htm$ https://thedj.com.au/testimonials/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://thedj.com.au/ [L,R=301]</ifmodule> END HTTPS Redirection Plugin BEGIN WordPress <ifmodule mod_rewrite.c="">RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]</ifmodule> END WordPress RewriteCond %{HTTP_HOST} ^mrdj.net.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.mrdj.net.au$
RewriteRule ^/?$ "https://thedj.com.au/" [R=301,L] RewriteCond %{HTTP_HOST} ^mrdj.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.mrdj.com.au$
RewriteRule ^/?$ "https://thedj.com.au/" [R=301,L] RewriteCond %{HTTP_HOST} ^thedjs.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.thedjs.com.au$
RewriteRule ^/?$ "https://thedj.com.au/" [R=301,L] RewriteCond %{HTTP_HOST} ^theperthweddingdjs.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.theperthweddingdjs.com$
RewriteRule ^/?$ "https://thedj.com.au/" [R=301,L] RewriteCond %{HTTP_HOST} ^thedjs.net.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.thedjs.net.au$
RewriteRule ^/?$ "https://thedj.com.au" [R=301,L]0 -
302 Redirect from HTTP to HTTPS
Hi Guys One of our client's website is having 586 linking unique domains to http://www.XYZ.com.au (to home page only). They have migrated their site to https://www.XYZ.com.au so all of their site pages are on HTTPS now instead of HTTP. The HTTP version of the home page is 302 redirected to HTTPS therefore we think they are not getting all the link juice of 586 linking domains and would like to recommend to change their 302 to 301. However we have not seen any ranking drop due to this migration and redirect in place. The new HTTPS site/redirect is live from last 2 months now. So not sure its worth recommending 301 or not? Does this mean Google is picking up this 302 redirect as normal and attributing all link value to HTTPS version? Please can anyone share their thoughts on recent Google interpretation of 302 from HTTP to HTTPS? Thanks
Technical SEO | | JamesDixon700 -
Redirect them to some other related pages in your website
dear sir, i found 404 errors in my google adsense account as well google webmaster tool. how can i escape from these errors. and also tell me why can i increase google click rate. my google rate is so low.
Technical SEO | | learningall0 -
Domain redirection and seo implications
We have an existing site that is a subdomain but we recently acquired an exact match domain. Will building links to the exact match domain and having the domain point at our existing subdomain work or should we convert the entire site and redirect our existing subdomain to the new domain? What I'm trying to figure out is how to maximize the benefit here and how the existing mass of links pointing to our existing subdomain (shop.domain.com) can be used. New domain: keywordshop.com Existing URL: shop.domain.com
Technical SEO | | CHarkins0 -
Multiple redirects a problem?
When product is sold out I will 301 redirect to a category page if a similar product is not available, but now our web developer has changed all the url's of the category pages so I need to redirect them all to the new category pages but that means there are some products that are first being redirected to the no longer existent category and then being redirected again to the new category page. This seems like it might me be a problem having two 301 redirects so I wanted to find out for sure if it is. Unfortunately our system for redirecting pages is archaic so it will be difficult and time consuming to go back and redo all the redirects that are going to pages that no longer exist so I wanted to get some additional opinions before I do that.
Technical SEO | | KentH0 -
301 Redirect Question
I'm working on a site that has a lot of indexed pages and backlinks to both domain.com and www.domain.com. Will using a 301 redirect to send domain.com to www.domain.com merge all of the indexed pages and links over to www.domain.com, thereby strengthening the www?
Technical SEO | | Yo_Adrian0