Can't Redirect http://www.mydomain.com/?q=node/4

Hi I’m a bit stuck with a rewriterule.
I have around 10 pages like these

http://www.mydomain.com/?q=node/12
http://www.mydomain.com/?q=node/78

And I’m trying to 301 redirect each one permanently to different pages, for example to: http://www.mydomain.com/about/

Everything I’ve tried up to now doesn’t work, here was my last effort…

RewriteCond %{QUERY_STRING} ^q=node/4$
RewriteRule ^$ http://www.mydomain.com/about/? [L,R=301]

Can any one suggest what I need to do to get this one working.
Thanks!

If you have certain number of urls then simply do as:

RewriteRule ^\\?q=node/4$ http://mydomain.com/node-4-url [R=301,L]
RewriteRule ^\\?q=node/8$ http://mydomain.com/node-8-url [R=301,L]
RewriteRule ^\\?q=node/12$ http://mydomain.com/node-12-url [R=301,L]

Thanks.
I’ve just started off with the first line that you gave me for node 4 and it doesn’t work. The htaccess file is definitely working, but not this line. All I get is the home page coming up (which I’m not redirecting to) and the old URL remains in the location bar. The only lines previous to this in the .htaccess are:

Options +FollowSymLinks
RewriteEngine On

Not sure why this is, is there a setting I’m missing or is there something that’s required when you’re not using a file name in the url?

Clutching at straws a bit here…why wouldn’t this work?

Googly,

As you discovered, PHPycho’s attempt to match the query string using a RewriteRule is doomed to failure (RewriteRule will only attempt to match the {REQUEST_URI} string).

Where you are failing is that you’re attempting to match a null string whereas Apache can’t serve that so it’s attempting to match (and failing) the DirectoryIndex in use at the time it’s examining the mod_rewrite code. Try changing your regex to ^(|{DirectoryIndex))$ (REPLACE {DirectoryIndex} with the file name) and see if that doesn’t do the trick.

Regards,

DK

Thanks for your advice guys, I’m still stuck but I think that’s because I didn’t explain myself at the beginning (not another one I hear you cry!) oops.

DK from reading your post I think I mislead you?? or else I’m not quite catching you

So here we go…I have redesigned a website that previously used something like drupal and I’m now using Joomla (on the same domain), so the old URLs now do not work and I need to permanently redirect them to the correct new pages.

Does that help matters at all? Hope so :slight_smile:

Googly,

Okay, let’s drop back, then, and have me ask for your “specification” for the rewrite job.

RewriteCond %{QUERY_STRING} ^q=node/4$
RewriteRule ^[COLOR="Blue"](|index\\.php)[/COLOR]$ http://www.mydomain.com/about/? [L,R=301]

obviously does not do it for you (with or without the blue bit?) and PHPycho was on the right track by inventing a redirect which CAN be done with the node/# of your example. My problem is that node/4 does NOT translate to about/.

Regards,

DK

Yes that works, without the blue in my case, thanks guys.

I’m pretty confused because the one that I ended up testing and getting right after my last post is the one I displayed in my original post!!

RewriteCond %{QUERY_STRING} ^q=node/4$
RewriteRule ^$ http://www.mydomain.com/about/? [R=301,L]

Well the only difference is that [R=301,L] is the other way around…would that make a difference? Would the L stop the redirect in my original post?

This was my original post:

RewriteCond %{QUERY_STRING} ^q=node/4$
RewriteRule ^$ http://www.mydomain.com/about/? [L,R=301]

Googly,

It makes no difference (except, to me, the Last flag should be last - prettier that way).

Regards,

DK

Very strange, looks like it must have been my error, although I did make sure I checked and double checked that it wasn’t working etc etc - sorry!

One thing that’s come along now is that on some of these pages I just wanted to force some 404s from OLD urls so that they will drop out of Google’s index, they’re just user log on pages - no equivalent on the new site.

So I did the following:

RewriteCond %{QUERY_STRING} ^q=user(.*)$
RewriteRule ^$ http://www.mydomain.com/? [R=404,L]

And this works and gives a 404 on the live site, but not on my local WAMP site. That produces a server error for the whole local test site. Same htaccess file uploaded (!) - off the top of your head do you know if there is a server setting on my local installation that might cause this error?

Googly,

If you don’t care what follows user in the query string, just leave the end anchor off (and get rid of the :kaioken: EVERYTHING :kaioken: atom).

Nice that you found that the ? in the redirection kills any existing query string.

Finally, use the 410 HTTP response code (for Gone) rather than 404 (not found).

Regards,

DK