Forum Moderators: phranque

Message Too Old, No Replies

301 redirect codes not working in existing htaccess file

301 redirect codes not working in existing htaccess file

         

Leads_Mgr

7:10 am on Oct 12, 2011 (gmt 0)

10+ Year Member



Hello...I've been dealing with this issue for about 72 hours non-stop and still cannot find the appropriate solution for my 301 issue.

The scenario is we just redesigned our company website and I cannot get the 301 redirect codes to work correctly. I get 404 errors for every url i wrote code for.

As far as I understand it, you are not supposed to modify or touch any existing existing code in the htaccess file. I simply added all the 301 codes at the bottom. Here is the a brief sample of the existing htaccess file, followed by what I added:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^videotracker.com
RewriteRule (.*) [videotracker.com...] [R=301,L]

Redirect permanent /wp-content [videotracker.com...]
Redirect permanent /avidexams [videotracker.com...]
Redirect permanent /index080818.php [videotracker.com...]

SetEnvIfNoCase User-Agent "^Access" bad_bot
SetEnvIfNoCase User-Agent "^Agent" bad_bot
SetEnvIfNoCase User-Agent "^Aleksika" bad_bot


<Limit GET POST>
Order Allow,Deny
Allow from all

Deny from env=bad_bot


#internetseer
Deny from 63.238.163.76

#spammer
Deny from 200.63.42.136
Deny from 91.224.160.92

AddType text/html .html
AddHandler server-parsed .html

AddType text/html .asp
AddHandler server-parsed .asp

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

ErrorDocument 404 [videotracker.com...]


HERE IS WHAT I ADDED DIRECTLY AFTER THE LINE ABOVE...WITH A LINE IN BETWEEN (ABOUT 100 LINES OF THE SAME CODE FOLLOW).

Redirect 301 /working.php?pg=avidexams [videotracker.com...]


Please help. We had about 1500 pages indexed and we really don't want to lose any business we've worked hard to build.

Thanks.

g1smd

7:21 am on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Change your code that uses Redirect permanent or Redirect 301 to instead use RewriteRule with the [R=301,L] flags. If you use RewriteRule for some of your rules you must use it for all of your rules.

Next order those rules from most specific (affects a single URL) to most general (the catch-all non-www to www rule). This is to avoid a non-canoical request generating a multiple-step redirection chain.


Make sure that every RewiteRule that is configured as a redirect has the protocol and domain name specified in the rule target.

If you have any RewriteRules that are configured as internal rewrites, these should be listed last (i.e. after the non-www/www redirect ruleset). An internal rewrite does not include protocol or domain name in the rule target and will use only the [L] flag.


Move your DENY code so that it is the very first lines of code in the file.


Be aware that your ErrorDocument directive is incorrect. The Apache manual specifically warns that you must NOT specify a domain name here. Your directive does not return 404 status. It reeturns a 302 redirect to a new URL which then returns 200 OK. Google warns that these "soft 404 errors" are taken as a signal of "low technical quality" when they evaluate a website.


Finally, you appear to be missing a </Limit> tag.

Leads_Mgr

7:36 am on Oct 12, 2011 (gmt 0)

10+ Year Member



THANK YOU SO MUCH for responding. I'm a newbie with these things so I apologize if the following questions are very basic:

- do I leave the upper portion (everything above and including the 404 rule) in tact and start below the 404 error code? This is all old code that someone else did, who knows how long ago.

-which is the correct code to use instead of the 301 command as it shows (/olddomain/fileA [newdomain.com...]


-so do I just delete the ErrorDocument 404 line altogether?

-how/where do I use the </Limit> tag? I see tag used just before the "Redirect 301" lines begin (about 30% of the way down).

g1smd

7:52 am on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to look at the whole file, and change the order as directed above.

1. The SetEnvIfNoCase lines followed by your DENY code (inside <Limit ...> ... </Limit> tags) Add the closing tag after the last DENY.

2. RewriteRules configured as redirects (most specific first). These have domain name in target and [R=301,L].

3. RewriteRules configured as rewrites (most specific first). These do NOT have domain name in target and use only [L] flag.

4. ErrorDocument /path/file directive (remove the domain name)

5. The other stuff (AddType, AddHandler etc).

To change the redirects edit the code as follows:
Redirect permanent /pattern http://www.example.com/somestuff

becomes
RewriteRule ^pattern http://www.example.com/somestuff [R=301,L]

Note especially the lack of leading slash in the RegEx pattern.

You cannot simply add stuff to the end of the file. The directives, as a whole, have to be in the right order.

Use example.com in this forum to suppress URL auto-linking.

Leads_Mgr

8:05 am on Oct 12, 2011 (gmt 0)

10+ Year Member



So I'm mixing the old code and the new code together? is this correct:

SetEnvIfNoCase User-Agent "^Access" bad_bot
SetEnvIfNoCase User-Agent "^Agent" bad_bot
SetEnvIfNoCase User-Agent "^Aleksika" bad_bot

<Limit GET POST>
Order Allow,Deny
Allow from all

Deny from env=bad_bot

#internetseer
Deny from 63.202.163.63

#spammer
Deny from 201.53.46.136
Deny from 92.254.120.92

</Limit>

RewriteEngine On
RewriteCond %{HTTP_HOST} ^videotracker.com
RewriteRule ^avidexams [videotracker.com...] [R=301,L]
RewriteRule ^index080818.php [videotracker.com...] [R=301,L]
RewriteRule (.*) [videotracker.com...] [R=301,L]

ErrorDocument 404 /error/error404.html

AddType text/html .html
AddHandler server-parsed .html

AddType text/html .asp
AddHandler server-parsed .asp

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

g1smd

8:09 am on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Edit your post to fix the RewriteRule code. Use example.com in your posts.

You have mashed the non-www/www redirect. The original RewriteCond/RewriteRule need to be together.

Add a blank line after every RewriteRule line.

Leads_Mgr

8:14 am on Oct 12, 2011 (gmt 0)

10+ Year Member



ok, is this correct?


SetEnvIfNoCase User-Agent "^Access" bad_bot
SetEnvIfNoCase User-Agent "^Agent" bad_bot
SetEnvIfNoCase User-Agent "^Aleksika" bad_bot

<Limit GET POST>
Order Allow,Deny
Allow from all

Deny from env=bad_bot

#internetseer
Deny from 63.202.163.63

#spammer
Deny from 201.53.46.136
Deny from 92.254.120.92

</Limit>

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteRule ^avidexams http://www.example.com/avid1 [R=301,L]

RewriteRule ^index080818.php http://www.example.com/avid2 [R=301,L]

ErrorDocument 404 /error/error404.html

AddType text/html .html
AddHandler server-parsed .html

AddType text/html .asp
AddHandler server-parsed .asp

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

Leads_Mgr

3:46 pm on Oct 12, 2011 (gmt 0)

10+ Year Member



I tried the coding above and is still doesn't work. Please help, Anyone willing to fix this for me for a fee?

g1smd

6:51 pm on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In what way does it not work?

What was the requested URL that did not work? (use example.com) What was it supposed to do? What did it actually do? How does this differ from what you expected?

Use the Live HTTP Headers extension for Firefox to see the HTTP transaction.

For the three listed redirects, the non-www to www redirect should be the last of the three.

Literal periods in RegEx patterns should also be escaped.

lucy24

8:48 pm on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So I'm mixing the old code and the new code together?

There is nothing sacred about old code. Every single time your server gets a request, it reads through the whole htaccess as if it had never seen it before.

Group everything conceptually. This is both for your benefit and apache's. It goes through your htaccess with very narrow blinkers, so if you have something like

SetEnvIf {blahblah}
RewriteRule {blahblah}
SetEnvIf {blahblah}
RewriteRule {blahblah}

it will pick up all the SetEnvIf directives on one pass, ignoring everything else; all the RewriteRule directives on another pass, and so on.

Unless you did the installation yourself, the order of modules is out of your control. But within each module the are executed in the order that Apache finds them. Keeping them together is the only practical way to make sure you've got them in the right order.

And never say "It doesn't work". ;) You know exactly what this means, but the people reading your post don't. See g1's list of possible translations above. At a minimum: What is supposed to happen, and what does happen?

Leads_Mgr

9:09 pm on Oct 12, 2011 (gmt 0)

10+ Year Member



"it doesn't work" was referring to the fact that I still get 404 errors on any of the old urls that I test.

Is the last bit of code correct or is there something missing or misplaced?

Thanks.

g1smd

9:15 pm on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So you request www.example.com/index080818.php and you are redirected to http://www.example.com/avid2

Have you got a matching file inside the server to actually deliver that content?

Leads_Mgr

9:32 pm on Oct 12, 2011 (gmt 0)

10+ Year Member



The redirected urls are pointed at a different server than the old urls. Does that make sense or am I wording that incorrectly?

g1smd

9:36 pm on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have no idea which is the old URL and which is the new, as you didn't say.

What was the requested URL that did not work? (use example.com) What was it supposed to do? What did it actually do? How does this differ from what you expected?

Leads_Mgr

10:06 pm on Oct 12, 2011 (gmt 0)

10+ Year Member



for example:

RewriteRule ^avidexams http://www.example.com/avid1 [R=301,L]

gives me a 404 error and then shoots me to the homepage as opposed to the new url specified in the line. None of the lines of code (there are about 100 redirects in the full file) redirected me to the correct new url. They all end up in 404 errors.

g1smd

10:11 pm on Oct 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What was the requested URL that did not work? (use example.com) That is, what is the URL that you typed in or clicked on at the very first step?