Forum Moderators: phranque

Message Too Old, No Replies

301's are failing me

Can you check this code for me?

         

trillianjedi

10:12 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Always had problems with this. It's all voodoo to me ;-)

Neither of these redirect instructions do anything:-


#Redirect any /index.php/ addresses to our normal one
Redirect permanent /index.php/ http://www.example.com/

Redirect 301 /index.php?file=profile&mode=viewprofile&u=3 http://www.example.com/myprofile.html

Any thoughts on what I've done wrong?

Thanks,

TJ

asantos

10:47 pm on Mar 11, 2006 (gmt 0)

10+ Year Member



Hi. Try this, works fine for me.
Put this lines in your old domain's root htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} $ [NC]
RewriteRule ^(.*)$ [newdomain.com...] [R=301,L]
</IfModule>

trillianjedi

11:06 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Asantos,

I don't quite follow you - that looks to me like it would just redirect everything to another domain?

That's not what I want to do - I only want to redirect two specific pages to their new location on the same domain.

TJ

jimbeetle

11:11 pm on Mar 11, 2006 (gmt 0)

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



Delete the trailing slash in the first example?

Redirect permanent /index.php/ http://www.example.com/

trillianjedi

11:22 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jim,

So you mean do this:-

Redirect permanent /index.php http://www.example.com/

?

I tried that and the site fell over (wouldn't load any pages at all), which was interesting!

I've tried both "Redirect permanent" and "Redirect 301" also.

This precedes the commands:-


# first let's deny the ip's we don't like
order allow,deny

<Various spammers in here ;-)>

allow from all

<Redirects start here>

As I understand it, .htaccess is read from the top down, so I don't think there can be anything lower down the file messing this up.....?

Thanks,

TJ

Key_Master

11:34 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond %{REQUEST_URI} ^/index\.php\?file=profile&mode=viewprofile&u=3$ [NC]
RewriteRule .* http://www.example.com/myprofile.html [R=301,L]

RewriteCond %{DOCUMENT_URI} index\.php$ [NC]
RewriteRule .* http://www.example.com/ [R=301,L]

By the way, if the new index file is named "index.php" this code will create an infinite loop, and the page will not load.

jimbeetle

11:41 pm on Mar 11, 2006 (gmt 0)

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



<edit>My post not at all necessary since somebody who actually understands this stuff came along.</edit>

trillianjedi

11:46 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Key_Master.

Thanks.

The first one didn't work. I preceded it with RewriteEngine On as I assume I should do?

I haven't tried the second as I take your point about infinite loops. Is there any way around that problem incidentally?

Jim - ah, you edited before I could reply. But yes, it is all voodoo to most of us I think.... ;-)

Key_Master

12:05 am on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I screwed up. I always make the mistake of using perl variables with htaccess. The following will work.

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^file=profile&mode=viewprofile&u=3$ [NC]
RewriteRule .* http://www.example.com/myprofile.html [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

trillianjedi

12:14 am on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I screwed up.

Bad KeyMaster!

The following will work.

Good KeyMaster!

Hey, it works! Awesome, thank you. No nasty loops either. What a damn useful bit of code that is.

Hmmmm.... educated in the ways of the Voodoo is this one :-)

Many thanks for your help - much appreciated.

TJ

Key_Master

12:28 am on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hehe, yep sorry. :) Glad it's working for you.

DOCUMENT_URI on Perl is equivalent to REQUEST_URI on Apache. Apache doesn't have a DOCUMENT_URI environmental variable.

REQUEST_URI on Perl is the requested document/path/query string.

You can see how confusing it is for someone who programs in Perl all day long. It would be so much easier if they were on the same page with respect to environmental variables.

trillianjedi

2:44 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know the feeling with different code syntax!

Got another slight problem. I assumed that I could have several of these, eg:-

RewriteCond %{QUERY_STRING} ^name=PNphpBB2&file=profile&mode=viewprofile&u=3$ [NC]
RewriteRule .* http://www.example.com/myprofile.html [R=301,L]

RewriteCond %{QUERY_STRING} ^name=PNphpBB2&file=profile&mode=viewprofile&u=2$ [NC]
RewriteRule .* http://www.example.com/hisprofile.html [R=301,L]

Again, I seem to get stuck in a loop - the second one seems to "stall".

Any thoughts, or is what I'm trying to do here impossible?

Thanks!

TJ

Key_Master

5:23 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're using the first line too (REQUEST_URI for index.php), right?

RewriteEngine on
Options +FollowSymLinks

RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^name=PNphpBB2&file=profile&mode=viewprofile&u=3$ [NC]
RewriteRule .* http://www.example.com/myprofile.html [R=301,L]

RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^name=PNphpBB2&file=profile&mode=viewprofile&u=2$ [NC]
RewriteRule .* http://www.example.com/hisprofile.html [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

jdMorgan

5:54 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also eliminate some unnecessary processing by moving the URL-pattern from a RewriteCond into the rule itself:

RewriteEngine on
Options +FollowSymLinks
#
RewriteCond %{QUERY_STRING} ^name=PNphpBB2&file=profile&mode=viewprofile&u=3$ [NC]
RewriteRule [b]^index\.php$[/b] http://www.example.com/myprofile.html [NC,R=301,L]
#
RewriteCond %{QUERY_STRING} ^name=PNphpBB2&file=profile&mode=viewprofile&u=2$ [NC]
RewriteRule [b]^index\.php$[/b] http://www.example.com/hisprofile.html [NC,R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

Jim

trillianjedi

6:12 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys.

Jim - OK, working as expected now, except that the server appears to be returning a 200 OK and the URL in the address bar shows the "old" URL.

From the WebmasterWorld header check tool:-

HTTP/1.1 200 OK
Date: Sun, 12 Mar 2006 18:05:46 GMT
Server: Apache/2.0.46 (Red Hat)
Last-Modified: Sun, 12 Mar 2006 12:31:30 GMT
ETag: "4d81ba-1437-67d4ec80"
Accept-Ranges: bytes
Content-Length: 5175
Connection: close
Content-Type: text/html; charset=UTF-8

That's when I enter:-

http://www.example.com?name=PNphpBB2&file=profile&mode=viewprofile&u=2

I've now got these lines right at the bottom of my .htaccess. Could it be something above them that's causing the 200 to be served rather than the 301?

Thanks again!

TJ

<Added>

Ah - it actually seems to be redirecting, but including the variables which are no longer required, so when I enter the original link:-

www.example.com?name=PNphpBB2&file=profile&mode=viewprofile&u=2

I get taken to:-

www.example.com/[b]hisprofile.html[/b]?name=PNphpBB2&file=profile&mode=viewprofile&u=2

.... and I get a 200 OK where I would expect to see a 301 in the header?

hisprofile.html is a static page - the additional variables are not required by it.