Forum Moderators: phranque

Message Too Old, No Replies

.ht access redirect with GET data

Redirecting GET data

         

Rob_Dickson

9:58 am on Jan 5, 2008 (gmt 0)

10+ Year Member



Hi guys. I'm new to Webmaster World. I have found the information in these forums very useful over the last year or two, but have only just needed to post.

I wonder whether anybody here can help me out. I'm sure I'm being stupid, but I just can't figure this one out.

All I want to do is to redirect www.mydomain.co.uk/link?id=n to www.mydomain/link/n (where n is a number).

I would have thought that something like these two lines would work:
Redirect 301 /old_dir/link.php?id=([0-9]+)$ http:/www.mydomain.co.uk/link/$1
RewriteRule ^old_dir/link.php?id=([0-9]+)$ http:/www.mydomain.co.uk/link/$1 [R]

The only way that I can find to do it is:
Redirect 301 /old_dir/link.php http:/www.mydomain.co.uk/linkredirect.php

This passes the?id data from link.php to linkredirect.php, and then use PHP's Header function to redirect to the page that I want.

Surely there must be an easier way than this. If anybody can help, I'd appreciate it.

PS: I have written http:/ instead of using two forward slashes to stop the forum code from changing the URLs into <a> tag URLs. I hope that's OK.

phranque

12:22 pm on Jan 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], rob_lurkson! =8)

if you use example.com, instead of mydomain then you won't get the <a> tags.
(example.com is never assigned.)

you're close!
the pattern is not matched against the query string in a rewriterule - only the url.
you want to use the rewritecond directive [httpd.apache.org] on the QUERY_STRING variable and then a %n backreference in the rewriterule.

if you still don't get it working after reading up and trying some things, post your latest rewrite code and results and you'll get plenty of help.

Rob_Dickson

1:19 pm on Jan 5, 2008 (gmt 0)

10+ Year Member



Thanks for your swift reply.

I'm going to have to read up a bit more on RewriteCond before modifying my .htaccess file. From what I've read so far, I am thinking about something along the lines of:

RewriteCond %{REQUEST_URI} /old_dir/link.php$
RewriteCond %{QUERY_STRING}?id=([0-9]+)
RewriteRule ^.*$ /link/%1 [R=301,L]

I've already got RewriteEngine On entered in my .htaccess file for other RewriteRules. Will the two RewriteCond lines only affect the single RewriteRule, or do I need to use some sort of "end of RewriteCond" statement?

Thanks again for your help.

PS: Why "rob_lurkson"?

phranque

1:58 pm on Jan 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



that looks pretty close - you might need to fix some pattern anchors and clear out the query string in the rewriterule with a?.

the rewriteconds only apply to the following rewriterule and the "L" option stops the rewriterule processing for that pass.

lurkson was simply a play on your name and a joke about your "lurking" here for a year or two.
(i hope i didn't offend)

Rob_Dickson

3:32 pm on Jan 5, 2008 (gmt 0)

10+ Year Member



Thanks for the reply.

I'm not sure exactly what you mean, but I'm sure I'll figure it out when I read up a bit more, and try playing around with the .htaccess file. I've found it hard going in the past, but always got there eventually.

I'll let you know how I get on, and post back if I need any more help.

No offence taken with "lurkson", I just didn't understand.

Rob_Dickson

1:17 pm on Jan 6, 2008 (gmt 0)

10+ Year Member



I got a 500 Internal Server Error error message when I tried it.

RewriteEngine On
RewriteCond %{REQUEST_URI} /old_dir/link.php$
RewriteCond %{QUERY_STRING}?id=([0-9]+)
RewriteRule ^.*$ /guid/mainsite/%1 [R=301,L]

I'm afraid that I don't know enough about .htaccess and rewrite rules to play about with the different parameters in a meaningful way. If someone could point me in the right direction, I'd appreciate it.

phranque

1:23 pm on Jan 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the server log files should have some clues.

Rob_Dickson

5:24 pm on Jan 6, 2008 (gmt 0)

10+ Year Member



RewriteCond: bad argument line '%{QUERY_STRING}?id=([0-9]+)'\n

I tried two changes, but neither helped.
RewriteCond %{QUERY_STRING}?id=([0-9]+)$
RewriteCond %{QUERY_STRING}?id=([0-9]+)%

If you have ideas, I'd appreciate your help.

I'm using Notepad to edit the .htaccess file, so I doubt that there are any spurious characters in it.

jdMorgan

10:06 pm on Jan 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING} &?id=([0-9]+)&?
RewriteRule .* /guid/mainsite/%1? [R=301,L]

The "?" preceding GET data is not part of the URL-path or the query string -- It is a delimiter between them, and so should not appear in either pattern.

Also, "?" in regular expressions patterns is a regex token meaning "zero or one of the preceding character." If a literal "?" is to be matched, then it must be escaped by preceding it with a backslash, i.e. "\?".

The "?" shown in the substitution URL-path is a mod_rewrite token which serves to clear the original query string, which will otherwise be appended by default.

Jim

Rob_Dickson

7:05 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



Thanks for your reply. I'm afraid I haven't had time to try out what you've suggested, or digest the information that you've given me.

I hope to have time this weekend, but just wanted to post to thank you for your help.