Forum Moderators: phranque
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.
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.
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"?
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)
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.
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.
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.
RewriteCond %{QUERY_STRING} &?id=([0-9]+)&?
RewriteRule .* /guid/mainsite/%1? [R=301,L]
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