Forum Moderators: phranque

Message Too Old, No Replies

Rewriterule to correct crawl errors

mod_rewrite, rewriterule

         

tmccar

6:10 pm on Mar 17, 2011 (gmt 0)

10+ Year Member



I'm getting lots of crawl errors in Google Webmasters, and many of them look like this:
[onlineglobalbiz.com...]

I would like to redirect this link to [onlineglobalbiz.com...] (shave off the strange URL tacked on at the end).

I have tried may mod_rewrite options in my .htaccess, especially with Rewriterule but I can't get it to work.
Any help would be greatly appreciated.

tom

g1smd

8:49 pm on Mar 17, 2011 (gmt 0)

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



I guess you have some malformed links within your site. Use
Xenu Linksleuth
and attempt to track them down. Those links must be fixed to point to the correct URL.


What have you tried so far? It should be just a couple of simple lines of code, something like:

RewriteRule ^(([^/]+/)+)www\.example\.com http://www.example.com/$1 [R=301,L]


Actually, if the problem is malformed links, this code will get the user to page they were expecting:

RewriteRule ^(([^/]+/)+)www\.example\.com/(.*) http://www.example.com/$2 [R=301,L]

tmccar

7:53 pm on Mar 18, 2011 (gmt 0)

10+ Year Member



Sorry - still getting "nothing found for... when I click on this link:
[onlineglobalbiz.com...]

g1smd

8:11 pm on Mar 18, 2011 (gmt 0)

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



For whatever reason, there is no redirect when the link is clicked.

That means the code is either incorrect, incomplete, or not installed in the right place.

tmccar

9:43 pm on Mar 18, 2011 (gmt 0)

10+ Year Member



It works now!
I edited .htaccess, so that the rewrite commands are before the Wordpress section, like so:

<IfModule mod_rewrite.c>

Options +FollowSymlinks
Options +Indexes
RewriteEngine On
RewriteRule ^(([^/]+/)+)www\.onlineglobalbiz\.com/(.*) [onlineglobalbiz.com...] [R=301,L]

</ifmodule>


# 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

But I would love to know why it works!
Thanks for your help
Tom

g1smd

10:37 pm on Mar 18, 2011 (gmt 0)

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



The rewrite code (the bit ending with the
[L]
flag) sends the request to the hard drive to get the content.

By then it is too late to change the URL with a redirect.

You should redirect incorrect requests first. The redirect tells the browser to make a new request for the new URL. Once the browser asks for the right URL, and only then, let the rewrite fetch the content.


You have repeated
RewriteEngine On
twice. The second copy can be deleted.

The
<ifModule>
code (4 tags) also isn't required.

tmccar

10:31 pm on Mar 24, 2011 (gmt 0)

10+ Year Member



Hi
Your fix is working very well for the many links I had in the form "www.onlineglobalbiz.com\<directoryname>/http://www.onlineglobalbiz.com/links.php"

But it falls down when there's more than one level in the directory path, e.g.: "www.onlineglobalbiz.com\<directoryname1/directoryname2>/http://www.onlineglobalbiz.com/links.php"

Could you help with this also please (so that it can handle any number of directory levels?)

g1smd

12:58 am on Mar 25, 2011 (gmt 0)

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



My example code make no provision for the http:// in the MIDDLE of the URL, so the pattern was probably matching one of the slashes in that.

Add that provision to the pattern. and then it should work for multiple depths. It "worked" despite a coding error!

tmccar

1:07 am on Mar 25, 2011 (gmt 0)

10+ Year Member



Like this:?
RewriteRule ^(([^/]+/+/+/)+)www\.onlineglobalbiz\.com/(.*) [onlineglobalbiz.com...] [R=301,L]

g1smd

1:11 am on Mar 25, 2011 (gmt 0)

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



NO.

Something like this:

RewriteRule ^(([^/]+/)+)http://www\.example\.com/(.*) http://www.example.com/$2 [R=301,L]

You'll likely also need to add provision for requests containing an encoded colon and/or encoded slashes.

Use example.com to stop the forum auto-linking your URLs. Without the autolinking on your post, you would have had the correct code in the first answer.

tmccar

7:56 pm on Mar 25, 2011 (gmt 0)

10+ Year Member



I'm sorry but would you please give the code for this?