.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 -
Javascript redirects harmful for SEO?
Hi Mozzers, A website deals with some javascript redirects. After some research, It came clear a normal 301 is the best option, but javascript redirects can be useful if you don't have access to a website' server. Does anyone have experience with javascript redirects? Can they be harmful in any point of SEO? I would love to hear your thoughts!
Technical SEO | | WeAreDigital_BE0 -
301 Redirect for multiple links
I just relaunched my website and changed a permalink structure for several pages where only a subdirectory name changed. What 301 Redirect code do I use to redirect the following? I have dozens of these where I need to change just the directory name from "urban-living" to "urban", and want it to catch the following all in one redirect command. Here is an example of the structure that needs to change. Old
Technical SEO | | shawnbeaird
domain.com/urban-living (single page w/ content)
domain.com/urban-living/tempe (single page w/ content)
domain.com/urban-living/tempe/the-vale (single page w/ content) New
domain.com/urban
domain.com/urban/tempe
domain.com/urban/tempe/the-vale0 -
301 Redirect Expert
I have hired two website people from odesk and had bad experience in 301 redirects. One never got it done and the other cause my website to crash on a 500 error code, he load all the redirects in a script on .htaccess on my wordpress site on godaddy, there were 1400 redirect.Can somebody recommend a expert for my redirect problem.
Technical SEO | | tjacquet0 -
Redirect Impact - Moving From SEOmoz to Moz
Hey Guys, This has been on my mind ever since the big announcement, so today I did some searching around for some posts/talk about what the impact of their full site redirect has been for them and didn't find anything. Have they posted on this or are there any threads that I'm missing out on? I'd love to hear more about what the impact has been or any general thoughts/insights people may have. Thanks!
Technical SEO | | TakeLessons
Jon1 -
Redirection Plugin and Regular Expressions
I am using Wordpress and the Redirection plugin. I have recently launched a new site and while it was in development, a lot of broken pages were created and indexed. These are terminating in 404s. I am looking to redirect all traffic from: /replay/postname/ to the homepage. When I use this regular expression: Source: /replay/* Target: / it removes the /replay/ but leaves the postname. Any idea how I could redirect all traffic inside of /replay/ to the homepage? Thanks a bunch, Josh
Technical SEO | | dreadmichael0 -
301 Redirect
Hi there, We are re-branding & re-structuring our website, there will be quite a number of 301 re-directs, possibly hundreds. The question is: Should i wait until the re-branding has been completed and do al the 301's in one go?, or should I try and do 301's as i go along? Kind Regards
Technical SEO | | Paul780 -
301 Redirects Change?
Hi, Mozzers- I've noticed pages that are getting 301 redirected are staying out there longer. It used to be that you would implement a 301 redirect and then after a couple of months the old page would disappear out of Google's index. Over the last couple of months I've noticed pages lingering, popping up.... What gives? Thanks in advance! LHC
Technical SEO | | lhc670