.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
-
Redirect By IP location
Hi All, I have a client who operates in multiple countries with the sub directory structure. In AU for their main brand name .com site still ranks in the first position but /au ranks for most of the other terms. Current we have a 301 redirect in place for .com for anyone accessing the site from AU to /au. This is only for home pages as other .com pages don't rank in Australia. Just wondering what implication this can have on our SEO campaign. Cheers
Technical SEO | | SSP21
Thank you for your expertise and insights in advance.1 -
How do you fix redirect chains and temporary redirects?
Hi, I have a lot of issues popping up with temporary redirects and redirect chains. I'm still confused as to what exactly redirect chains are and I don't know how to find where the "chains" are or how to fix them. I'm having two issues mainly:1. Temporary RedirectsI have around 100 pages on our www.twowayradiosfor.com website that are being flagged as temporary redirects. All of them have one thing in common: they are review pages (basically, when a customer clicks on the Review button to review a certain product, they are redirected to a review page for that product).URL Example: https://www.twowayradiosfor.com/reviewhelpful.asp?ProductCode=CLS1410-COMBO&ID=44&yes=noI went into our website and set any URL containing the following as noindex:/review.aspWill that fix the issue? If yes, will I also need to do that for any URL containing /reviewhelpful.asp?2. Redirect ChainsIt seems like basically every product page on my website has this issue (over 100 pages). Here's an example of one:https://www.twowayradiosfor.com/Motorola-CLS1110-p/cls1110.htmI don't see any broken links on this page or links that redirect to another page that redirects, etc. What is causing this? Is it something on my header bar that is redirecting (since that header bar appears on every page, maybe that is why this issue shows up on a lot of pages)?I am new to Moz and still trying to figure this stuff out. I really appreciate any help. Thanks, Sawyer
Technical SEO | | AllChargedUp0 -
Setting Up A Website For Redirects
I've got an old defunct domain with a lot of backlinks to individual pages. I'd like to use these backlinks for link juice by redirecting them to individual pages on the new domain (both sites belong to the same company). What is the best way to set this up? I presume I need some kind of hosting & site, even if it's just a default Wordpress install, which I can then use to set up the redirects? Would it be best done using .htaccess file for 301 redirects or some other way?
Technical SEO | | abisti20 -
Sub Domain Redirect
Hey Everyone, Here is the situation : Currently, a website's sub domain is being redirected to the main website home page. We're having issues getting the sub domain pages indexed. Just want to confirm that it is because of the redirect on the sub domain URL. Should we kill the sub domain redirect and set it up as it's own page? Will that solve the indexing issue for the sub domain pages. More explanation below: subdomain.domain.com currently redirects to domain.com We're having issues indexing pages belonging to the sub domain ( subdomain.url.com/page1 or subdomain.url.com/page2) Appreciate your input in advance. Cheers,
Technical SEO | | SEO5Team0 -
Redirect a 301 Redirect
Does any link juice get passed from a permanent redirect to a new 301 redirect? If so, are there any studies which indicate an estimated percentage?
Technical SEO | | RedCaffeine0 -
Geo-Redirection
Our client has two almost identical sites targeting: Australia (www.mysite.com.au) Rest of World (www.mysite.com) Currently they have splash page on www.mysite.com asking users to select: Australia Rest of World (redirects to: www.mysite.com/home) I'm thinking they should get rid of slash page and simply auto detect if user on www.mysite.com is based in Australia and serve a message "Do you want to visit our .com.au site. It's not helped by the fact the .com site appears to get served ahead of .com.au iin australia as both sites are hosted in US. Looking to change this! Thanks in advance for your help!
Technical SEO | | steermoz70 -
Rel=cannonical + 301 redirect
Hi All I am currently working on a DotNetNuke site. I have enabled friendly URL's which have changed the url structure from the default setting of TabId=x to whatever the page name is set as. I will use the following page as an example - www.notarealdomain./graphicdesign.aspx Now I would like to know if it would be worth my time to change this to "/graphic-design.aspx through the use of a 301 redirect and/or a rel=can. Any help would be much appreciated. Thanks
Technical SEO | | masterpete0 -
Question about domain redirects
One of my clients has an odd domain redirect situation. See if you can get your head round this: Domain A is set-up as a domain alias of Domain B Entering domain A or domain B takes you to default.asp on domain B. The default.asp includes VB script to check the HTTP_HOST variable. It checks whether the main doman name for domain A is present in the HTTP_HOST and if so redirects it to domain A/sub-folder/index.htm. If not present it redirects to domain B/index.htm. In both cases the redirect uses a response.Redirect clause. I think what is trying to be achieved is to redirect requests to Domain A to a sub-folder of Domain B. It works but seems extremely convoluted. Can anyone see problems with this set-up? Will link juice be lost along the redirect paths?
Technical SEO | | bjalc20110