Welcome to WebmasterWorld Guest from 18.208.159.25

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

htaccess query string redirects

Problem redirecting URLs with query strings.

     
9:23 pm on Dec 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 30, 2003
posts:12
votes: 0

I'm sure this has been answered, but I searched the board and can't find the answer.

First problem:
When someone goes to http://domain.com/index.php?p=200&more=1 they are not getting a 404. They are going to http://domain.com/ I have not set up any redirects for that.

Second problem:
I need to redirect http://domain.com/index.php?p=200&more=1 to http://domain.com/archives/200.php I haven't found any way that works correctly. The forward I was trying to use kept forwarding http://domain.com/index.php to http://domain.com/archives/200.php

9:34 pm on Dec 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 15, 2003
posts:2412
votes: 5


Here's a thread that might be helpful, i had a similar problem a little while ago:

RewriteRule for "example.com/?searchstring" [webmasterworld.com]

/claus

12:29 am on Dec 2, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 30, 2003
posts:12
votes: 0


I had already found that in a search, it returned a 500 ISE no matter what I did..
12:57 am on Dec 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Chris_m,

Welcome to WebmasterWorld [webmasterworld.com]!

The code you reviewed had a problem --- posting on this board removed the required spaces preceding the "!" character in that post. Based on your trouble, I have edited that post to correct the problem.

The following sample should get you a bit closer: This does an external redirect to meet the requirements you posted. However you may not want an external redirect. This code is intended for use in .htaccess and won't work properly in httpd.conf. Also, it only handles the one specific case you posted, not the general case of "p=n" redirecting to "archives/n.php" -- again, it wasn't clear if you wanted a general or specific solution.

We try not to "write your code for you" here (see the Charter link above), but this should get you going in the right direction. Specific questions about problems with your code are welcome.


RewriteCond %{QUERY_STRING} ^p=200&more=1$
RewriteRule ^index\.php$ http://www.example.com/archives/200.php [R=301,L]

Ref: Introduction to mod_rewrite [webmasterworld.com] (and follow the links)

Jim

1:43 am on Dec 2, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 30, 2003
posts:12
votes: 0


What I had been trying was (found on this board)

RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=78598894&more=1$ [NC]
RewriteRule ^index.php$ http://www.domain.com/archives/000124.php? [R,L]

But like everything else it has no reaction.

I don't understand why when someone goes to [domain.com...] they don't get a 404.

2:55 am on Dec 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Chris_m,

You won't get a 404 if index.php exists. The query string (everything following the "?") is *not* part of the resource name, it is a query to be passed to that resource. Therefore, the 404 test does not look at the query string at all -- If the index.php file exists, no 404.

The query string is handled separately, as further evidenced by the fact that you have to test it separately in the rewrite code using RewriteCond.

Try adding "Options +FollowSymLinks" ahead of "RewriteEngine on". If that doesn't work, have a look at your error log file to see if it says anything.

Jim

3:39 am on Dec 2, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 30, 2003
posts:12
votes: 0


this worked

RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=78598870&more=1
RewriteRule /* http://aphotolog.com/archives/000124.php [R,L]

but only after I capitalized "query_string".

Can someone explain tome where and which things should be capitalized and does it matter? or was it a fluke?

4:56 am on Dec 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Chris_m,

In Unix, *nix, and Apache -- and especially in mod_rewrite -- capitalization matters a lot. Forget 'personal coding style,' and do it exactly like it shows in the book [httpd.apache.org].

Also, be careful with that [NC] flag - too often, people use it 'by habit' and get into trouble. If your query string is generated as part of the output for a previously-visited page, then there's no reason to make the pattern-match case-insensitive since your script will never generate any uppercase variants.

With mod_rewrite, it's all in the details.

Glad you got it working!

Jim

5:59 pm on Dec 2, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 30, 2003
posts:12
votes: 0


Thank you, if it wasn't for your example and the links, I might as well gone on doing it in lowercase and managed to actually push myself over the edge insane trying to get it to work.

:)