.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 Redirects Showing As 307 Redirects
Hi, Our clients are adamant that they have set up 301 permanent redirects on their websites, but when we check using Screaming Frog and various online HTTP status code checkers they are showing as 307 temporary redirects. Examples;
Technical SEO | | Webpresence
http://www.lifestylelifts.co.uk/home-lifts/
http://www.terrylifts.co.uk/ Again, the client says they are seeing 301 redirects. Why are we seeing 307's? Who is right? Very puzzling, any theories would be very much appreciated 🙂 Thanks in advance. Lee.0 -
Using Temporary Redirects for Ecommerce Categories?
Hi I'm currently going over old category pages for an ecommerce website, and I'm trying to work out the best way of handling old pages. I will be setting up 301s for the majority of pages, as these are 100% dead and gone. I'm struggling a bit with certain pages though, whereby the category is empty but there is always the possibility that our buyers will purchase these products again in the future (or they might not, there's no way to tell). I know that this isn't what a 302 is for, but I'm wondering which would be better in this case: to create a 302 redirect or to do a full 301 and if the products are repurchased at a later date to create a whole new url. Hope that makes sense. Thanks, Kate
Technical SEO | | Lisaangel0 -
CGI Redirects
Trying to 301 old legacy files like oldsite.com/green/red.cgi/blue/ using this htaccess code: Redirect 301 /green/red.cgi/blue/ http://www.newsite.com/summary-page/
Technical SEO | | SoulSurfer8
Instead it's redirecting to: newsite.com/red.cgi/blue/ FYI oldsite.com's htaccess file does not have any global 301 rules that would conflict Does anyone know if cgi files require a different 301 syntax? Thanks!0 -
A script to automatically write 301 redirect rules to htaccess?
I was wondering if anyone could help provide some resources on how to automatically write 301 redirect rules to htaccess. Allow me to explain... I'm building a new website and the primary users are businesses. They have their own profile pages on the site. The URL is based off of their Company Name. In the event that they decided to change their name... reasons being, perhaps they mispelled it the first time, or they're removing LLC or adding Inc, I want to also change the URL and redirect the old URL to the new URL. Since the URL is based off of their Company Name, making a change to the company name would make a change to the URL. I know it doesn't have to work this way, but for our purpose this works best. In case the old URL had any links to it, I wanted to see if there was an way to automatically update an htaccess file with a 301 redirect from the old URL to the new one. Could anyone point me in the right direction of how to do this? Perhaps a sample script. I've done a lot of searches on Google and can't seem to find anything. e.g. Original:
Technical SEO | | bimmer540
Name: XYZ Widgets
URL: website.com/xyz-widgets New - business changes their company name in their profile:
Name: XYZ Widgets, Inc.
URL: website.com/xyz-widgets-inc Upon the user saving the changes in their profile, I'd like to write a 301 redirect to an htaccess file:
Redirect 301 /xyz-widgets http://www.website.com/xyz-widgets-inc I know how to manually write redirects and I've got a pretty smart web developer. We've just never triggered a script to automatically write to an htaccess file before. Is this possible? Any resources are appreciated. Any security risks? Thanks!0 -
What would happen if 301 redirects were not in place
Good Morning from 14 degrees C sunny Wetherby UK 🙂 My question is please.... "When a new site is given a total makover ie old urls are re written to radically different ones I know if you dont set up 301 redirects the infamous 404 error page will rear its head. But i wonder if 301 redirects were not configured how long on average does it take google to index the new site and serp links finally point to the new site". Thanks in advance 🙂
Technical SEO | | Nightwing0 -
Is this a safe 301 redirect?
We are moving our site from one platform to another. Currently on our site we have two homepages. "www.homepage.com" and "www.homepage.com/Index" Both pages have some high quality links pointing in on them. The problem: We are going to be doing a 301 redirect from "www.homepage.com/Index" page to "www.homepage.com" as we are moving platforms at this time we weren't going to create a "www.homepage.com/Index" page all. This leaves this page as an empty URL. With this webpage disappearing all together will we lose traction as we are redirecting an empty URL? Or is it better to recreate this "www.homepage.com/Index" on our new platform redirect it and wait for google to deIndex this page for us? As well is there a tutorial for how to implement 301 redirects or is this something worth looking for a developer and pay someone to do?
Technical SEO | | HCGDiet0 -
When should you turn off redirects to your new domain?
Our website moved to a new domain a year ago, and we have our original domain to redirect to our new domain. We're working on contacting people who still link to our old domain to ask them to update, but 7% of our traffic is still coming as a redirect from our old domain. My question is, when should we just shut the old domain down entirely and stop redirecting people to our new domain? Or should we just keep it up indefinitely? What would be the positive or negative impact on our new domain's SEO if we shut the old domain down? Thanks!
Technical SEO | | UWPCE0 -
301 Redirect Help
Hello! I am getting ready to launch my freshly coded site in the next week or so. My product URLs are changing SLIGHTLY and want to confirm I am going about things the right way: A. My LIVE site store URLs look like http://hiphound.com/shop/dog-collars . My DEV site store URLs look like http://hiphound.com/dog-collars . No /shop directory. B. The dev firm installed the rewrite rule below: ############################################ enable rewrites Options +FollowSymLinks RewriteEngine on #RedirectMatch 301 ^/shop?/$ http://hiphound.com/ RedirectMatch 301 ^/shop?/$ http://hiphound.com ########################################### C. When I manually enter a URL with /shop in the address the website redirects to the correct page which is good. QUESTIONS I HAVE 1. Is the above redirect correct? I need them to permanent. Don't think the above is right... 2. Will links in the Google index be redirected as well? I am assuming yes but just want to confirm. 3. For each page indexed in Google will its pagerank, etc. be passed to the new page using just the 301 above? 4. Do I need to create addtional 301s for each page? So mapping the old page to the new page? Please advise. The goal here is to of course preserve the rankings of the pages already in the Google index. THANK YOU!!! Lynn
Technical SEO | | hiphound0