Forum Moderators: phranque

Message Too Old, No Replies

Help with 301 redirect / rewrite rule

301 redirect, rewrite rule

         

NaturalEnquirer

2:12 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



I hope someone with rewrite expertise can help me as I don't know how to do this - I don't want to lose my existing google index entries!

I have old gallery2 site with links that were
http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html

New link should be
http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php


So I tried adding in root .htaccess file:

Redirect 301 /galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php


which does at least let Google redirect the link, but now the link displayed on the page is:

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php?album=fauna/insects/moths/adults/Luna_Moth_eye.jpg.html


I'm thinking I should be using Rewrite Rule, but I don't have the knowledge on know how to write the rule - I've made some attempts but can't get it to work.

I'm asuming I should put the .htaccess in the root and not the zenphoto directory?

Could someone please give me an example for this file? Then I think I can fix the oher links.

I'm really struggling with this!

g1smd

2:44 pm on Oct 5, 2010 (gmt 0)

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



Yes. Use RewriteRule.

If you add a question mark to the end of the target URL of the rule, it will not re-append the query string.

There are several threads today and yesterday discussing the same sort of redirect that you require and those will be good for example code.

sublime1

3:34 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



Hi NaturalEnquirer --

Here's a thread link from this week you should study: [webmasterworld.com...]

While the original request was for single image, the replies should get you going with a single example (always a good place to start :-).

When you're ready to move to doing the rewrite for all images you'll need to know how regular expressions work so you can make a general rule that says "for all URLs that end with ".jpg.html" send an external redirect that changes the extension to ".jpg.php'.


(And on a slightly unrelated note ... do the images really want to be known as ".jpg.php" to the world? A lot of programs use file extensions to determine what type of file it is -- it might be better to have your image files end with just ".jpg". If this sounds like something you want to do, it can probably be accomplished at the same time as you make the RewriteCond and RewriteRule you'll be working on anyway, all keeping Google love alive).

Tom

NaturalEnquirer

10:28 pm on Oct 6, 2010 (gmt 0)

10+ Year Member



Getting closer ... I now have:

RewriteEngine on
RewriteRule ^/galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html$ http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php? [L,R=301]


which now redirects

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html 


to

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php


but actual link is now

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php?


I need it to be

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php


Is there a way to strip the ? after the .php ?

Or does this matter in terms of Google index? Is
foo.php
same as
foo.php?
?

And I thank you for all the replies so far - really helpful (and needed) by me!

jdMorgan

1:22 am on Oct 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That question mark would not be expected to be appended by your RewriteRule, which is correctly coded. The question mark in your code should function only as an operator to clear any appended query string, but should not appear as a literal in the actual redirected-to URL.

However, you may be fooling Apache itself due to the "wonky" double-filetype in your URLs.

For this and other reasons, I would warn you that this double-filetype approach is going to cause trouble.
And in fact, it is likely not even needed.

If you need to send image requests to a .php script, then use an internal Rewrite, link to a "normal" URL like
http://example.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg
and then internally rewrite that URL to your script filepath at
/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php

As in many cases here, we can help correct your code, but that is no guarantee that the solution that the code will now successfully implement is actually the correct or desirable solution to the root problem...

Jim

[edited by: jdMorgan at 4:46 pm (utc) on Oct 15, 2010]

NaturalEnquirer

5:12 am on Oct 7, 2010 (gmt 0)

10+ Year Member



As to the .jpg.html and .jpg.php extension, the file is not an image, but is a php/html page with image and text on the page. As to the extension, that's the way the gallery came set up so I'm not planning for now to mess with that!

Back to my problems. I thought I got the above rewrite rule to work, but evidently not. So I'm back to:

Redirect 301 /galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php


which displays

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php?album=fauna/insects/moths/adults/Luna_Moth_eye.jpg.html


or

Redirect 301 /galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php?


which displays

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php?


Both of these links do go to the correct page, but the normal page links to

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php
.

From what you're saying, option 2 with ending question mark is not a good idea so ...

My question is does Google know that

http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php


is the same as

/galleries/fauna/insects/moths/adults/Luna_Moth_eye.jpg.html http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-eye.jpg.php
?

I don't want to lose my established Google links so this matters. And I really would prefer that user show the friendlier link.

Frankly I am totally confused at this point!

Again thanks to anyone who can help me with this mess.

jdMorgan

3:23 pm on Oct 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the RewriteRule to tell Google that the URL with the "?" at the end should be replaced by the one without. The Redirect directive is not capable of this function, as you have discovered.

Correct the gallery script to prevent it from publishing links with a trailing "?" on your pages. There is nothing you can do in server config code to fix these published links -- They must be fixed where they are created and published.

Jim

NaturalEnquirer

6:52 pm on Oct 7, 2010 (gmt 0)

10+ Year Member



Are you saying that I need to put this in the gallery index's htaccess file?

When I try the rewrite (in root directory) absolutely nothing seems to change. In the meantime, the luna moth eye link has not only dropped from the first page, but is completely gone from the google index!

So this is what I have tried (another example file since other one has disappeared from search):

RewriteEngine on
RewriteRule ^/galleries/fauna/insects/moths/adults/Luna_Moth_Eyes.jpg.html$ http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-Eyes.jpg.php [L,R=301]


I've put this at end of htaccess and the link doesn't change at all.

Really getting desperate now... just don't know what else to do other than watch all the links disappear and start over from scratch again!

jdMorgan

12:37 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



 RewriteEngine on
RewriteRule ^/galleries/fauna/insects/moths/adults/Luna_Moth_Eyes.jpg.html$ http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-Eyes.jpg.php [L,R=301]


I've put this at end of htaccess and the link doesn't change at all.

If you're putting the code into /.htaccess, then the syntax must change:

RewriteEngine on
#
RewriteRule ^galleries/fauna/insects/moths/adults/Luna_Moth_Eyes\.jpg\.html$ http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-Eyes.jpg.php? [R=301,L]

With the most important change being the removal of the leading slash from the RewriteRule pattern.

Listings disappearing and links disappearing are two different things. Try not to panic, as this will be utterly self-defeating. Your search listings will likely return unharmed within a few days of getting this code right. So worry and panic will only delay the "getting this code right" part, and therefore delay the return of your listings... If you deal in search engines and SEO, you need a cold, analytical, calculating attitude... This is no place for emotions.

Jim

NaturalEnquirer

3:30 pm on Oct 8, 2010 (gmt 0)

10+ Year Member



Jim,

First, I want to thank you for not giving up on me (yet)! I'm not that SEO-conscious, but if I have links on the first page, I sure would like to keep them there! I just didn't expect such a problem in what I thought was going to be a few simple redirects/rewrites ...

I copied your suggested code to the end of the root .htaccess file and nothing changes - displays unchanged link:
http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna_Moth_Eyes.jpg.html


Just in case this tells you anything else, on the album page itself, the error it displays is:

The page you are requesting cannot be found.
Album: fauna/insects/moths/adults
Image: Luna_Moth_Eyes.jpg.html


I also tried it on a new root .htaccess file with nothing else in it other than

AddHandler x-httpd-php5 .php
AddHandler x-httpd-php .php4


So maybe I need to do this in the \galleries .htaccess file, but didn't want to do it without advice. I've copied it below. If I change this .htaccess file (with a backup of original in place), will I have to do what it's talking about in the first few lines?

# htaccess file version 1.2.8.0;
# When Zenphoto requires changes to the rewrite rules:
#First make a copy of this file as 'oldhtaccess' in the zp-core folder so setup can test for unmodified files
#Update the above and the define in setup.php


<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /galleries

RewriteRule^admin/?$ zp-core/admin.php [R,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^albums/?(.+/?)?$ $1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

RewriteRule index\.php$ index.php [L,QSA]
RewriteRule ^page/([0-9]+)/?$ index.php?page=$1 [L,QSA]
RewriteRule ^page/([A-Za-z0-9_\-]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^page/([A-Za-z0-9_\-]+)/([0-9]+)/?$ index.php?p=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/page/([0-9]+)/?$ index.php?album=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/page/([A-Za-z0-9_\-]+)/?$ index.php?album=$1&p=$2 [L,QSA]

#### Rewrite rule addition for search
RewriteRule ^page/search/fields([0-9]+)/(.*)/([0-9]+)/?$ index.php?p=search&searchfields=$1&words=$2&page=$3 [L,QSA]
RewriteRule ^page/search/fields([0-9]+)/(.*)/?$ index.php?p=search&searchfields=$1&words=$2 [L,QSA]
RewriteRule ^page/search/archive/(.*)/([0-9]+)/?$ index.php?p=search&date=$1&page=$2 [L,QSA]
RewriteRule ^page/search/archive/(.*)/?$ index.php?p=search&date=$1 [L,QSA]
RewriteRule ^page/search/tags/(.*)/([0-9]+)/?$ index.php?p=search&searchfields=tags&words=$1&page=$2 [L,QSA]
RewriteRule ^page/search/tags/(.*)/?$ index.php?p=search&searchfields=tags&words=$1 [L,QSA]
RewriteRule ^page/search/(.*)/([0-9]+)/?$ index.php?p=search&words=$1&page=$2 [L,QSA]
RewriteRule ^page/search/(.*)/?$ index.php?p=search&words=$1 [L,QSA]
#### Rewrite additions for zenpage
RewriteRule ^pages/?$ index.php?p=pages [L,QSA]
RewriteRule ^pages/(.*)/?$ index.php?p=pages&title=$1 [L,QSA]
RewriteRule ^news/?$ index.php?p=news [L,QSA]
RewriteRule ^news/([0-9]+)/?$ index.php?p=news&page=$1 [L,QSA]
RewriteRule ^news/category/(.*)/([0-9]+)/?$ index.php?p=news&category=$1&page=$2 [L,QSA]
RewriteRule ^news/category/(.*)/?$ index.php?p=news&category=$1 [L,QSA]
RewriteRule ^news/archive/(.*)/([0-9]+)/?$ index.php?p=news&date=$1&page=$2 [L,QSA]
RewriteRule ^news/archive/(.*)/?$ index.php?p=news&date=$1 [L,QSA]
RewriteRule ^news/(.*)/?$ index.php?p=news&title=$1 [L,QSA]

RewriteRule ^(.*)/image/(thumb|[0-9]{1,4})/([^/\\]+)$ zp-core/i.php?a=$1&i=$3&s=$2 [L,QSA]
RewriteRule ^(.*)/image/([^/\\]+)$ zp-core/i.php?a=$1&i=$2 [L,QSA]
RewriteRule ^(.*)/album/(thumb|[0-9]{1,4})/([^/\\]+)$ zp-core/i.php?a=$1&i=$3&s=$2&album=true [L,QSA]
####

# Catch-all - everything else gets handled in PHP for compatibility.
RewriteRule ^(.*)/?$ index.php?album=$1 [L,QSA]
</IfModule>

jdMorgan

7:08 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can put the code into /galleries/.htaccess, only if there are no internal rewrites in /.htaccess that will match the /galleries URLs and apply an internal rewrite. Be sure to check this very carefully, as a failure to heed this caveat can make a fur-ball that will be painful to unravel.

That said,

RewriteRule ^fauna/insects/moths/adults/Luna_Moth_Eyes\.jpg\.html$ http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-Eyes.jpg.php? [R=301,L]

placed as the first rewriterule in /galleries/.htaccess will be equivalent to the previously-posted rule for use in /.htaccess

Note that your current first two rules in /galleries/.htaccess are variously missing the [R=301] and the full target URL-path. Using the immediately-preceding example rule, you should correct your existing rules.

Be sure to delete your browser cache before testing.

Jim

NaturalEnquirer

8:00 pm on Oct 8, 2010 (gmt 0)

10+ Year Member



To make sure I understand you exactly since I definitely don't want any "furballs":

1) My htaccess in root folder should not contain any rewrite rules (which it won't).

2) Are regular 301 redirects in root htaccess ok?

3) Do I place these rules here:

RewriteEngine On
RewriteBase /galleries

RewriteRule ^fauna/insects/moths/adults/Luna_Moth_Eyes\.jpg\.html$ http://natureinfocus.com/galleries/fauna/insects/moths/adults/Luna-Moth-Eyes.jpg.php? [R=301,L]

RewriteRule^admin/?$ zp-core/admin.php [R,L]


4) As to "furballs" can these htaccess changes be undone by restoring original htaccess (if done right away)? Sure don't want to make annoyance into nightmare.

Everything in the galleries .htaccess was created by the zenphoto install - as you can tell, I sure don't have skills to even come close to writing any of that stuff!

I will let you know the outcome (hopefully for final resolution). And thank you again.

g1smd

10:25 pm on Oct 8, 2010 (gmt 0)

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



The rule currently with [R,L] should have [R=301,L].

Any rule with R=301 should be coded so that the redirect target specifies both the protocol and domain name.

sublime1

1:10 am on Oct 9, 2010 (gmt 0)

10+ Year Member



The rule currently with [R,L] should have [R=301,L]


I was just reading the apache rewriting docs again for the 79,438th time and most of their examples fail this simple rule.

For who knows what reason, the default for a redirect is 302, not 301, and the docs are still consistently misleading people, even with the most basic domain canonicalization example, after 15 years.

I attempted to submit some corrections on this topic to the rewriting guide in around 2003 -- giving back to the open source world, I hoped. All of my corrections (which I think were actually correct) were rejected. Sigh.

Now jdMorgan and g1smd are in a "groundhog day" scenario of endlessly repeating the same corrections, clarifications and completions.

We all owe them both a large debt of gratitude!

g1smd

5:25 pm on Oct 9, 2010 (gmt 0)

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



Yeah, I know that rejection. I have offered multiple times to speak at SEO conferences on this stuff and been turned down. It's getting too late to bother, as most SEO conferences now seem to be populated by marketing types rather than the technical hands-on people.

NaturalEnquirer

2:19 am on Oct 11, 2010 (gmt 0)

10+ Year Member



Thanks to all for the help in getting my redirects straightened out - really appreciate your efforts. Everything seems fine now. Kudos to all.

g1smd

5:55 pm on Oct 11, 2010 (gmt 0)

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



All of your patterns with (.*) parts within can be optimised a lot further. This will significantly speed up the operation of the rules.

jdMorgan

4:56 pm on Oct 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Example:
 RewriteRule ^page/search/fields([0-9]+)/(.*)/([0-9]+)/?$ index.php?p=search&searchfields=$1&words=$2&page=$3 [L,QSA] 

will run a lot faster as:
 RewriteRule ^page/search/fields([0-9]+)/([^/]+)/([0-9]+)/?$ index.php?p=search&searchfields=$1&words=$2&page=$3 [L,QSA] 

or as a second-best choice, if your "page" name can contain slashes:
 RewriteRule ^page/search/fields([0-9]+)/(([^/]+/)*[^/]+)/([0-9]+)/?$ index.php?p=search&searchfields=$1&words=$2&page=$4 [L,QSA] 


Be sure that your script checks the originally-requested URL and externally redirects either the trailing-slash or no-trailing-slash non-canonical URL to your choice for the canonical URL. If your script cannot do this, then make the trailing slash in your above internal RewriteRules non-optional or remove it, and then write additional rules to force canonicalization of these URLs. Otherwise, you risk duplicate content, with each page accessible by two URLs: One with and the other without a trailing slash. That one error could cut that page's actual PageRank in half (Note that toolbar PageRank is a logarithmic representation of actual PageRank, though).

Jim