views:

308

answers:

4

I need to redirect a single Wordpress URL that is formatted like this: www.bluewidgets.com/?p=123 to a clean URL on another domain. How can I do this via .htaccess? All of the tutorials I've seen say that I need to specify another part of the url, like index.php, before the query string, but my URL doesn't have one - it's just the domain and then the query string.

A: 

Doesn't wordpress already have built in URL prettyfication? Just out of curiosity why would you need to do this.

Aside from that, yes what you mention is how you'd do it via htaccess. Are you encountering an issue with this method?

Joel Martinez
The problem is that I have two domains: domain1.com and domain2.com.I originally had my blog on domain1.com, then the actual name of my site became available to register so I registered domain2.com and made that my primary domain and parked domain1.com on top of it.That particular post - domain1.com/?p=xxx - has a paid post on it and the URL has to remain there for me to be paid monthly. However, I now want to separate domain1.com and domain2.com and use domain1.com for other purposes. So I need to implement a redirect from that domain1 post URL to the domain2 post URL
Liza
And what I mention seems to require that there be another bit to the URL, like /index.php?p=xxxx - as you see my URL does not have the index.php bit, just the /?p=xxxx so when trying that method it hasn't worked.
Liza
A: 

A standard 301 redirect is in the format (and would be at domain1):

Redirect 301 /filename.php http://domain2.com/filename.php

but I just tried that with default permalinks and it didn't work.

On domain1, you could rewrite the URL with the standard wordpress rewrite block, and then immediatly redirect it tp domain2. Messy, but it might work.

songdogtech
I tried the code you posted earlier, it doesn't seem to work with query strings. Not sure how to rewrite the URL the way you mean :S
Liza
Do you have wordpress at domain1? Or something else that uses mod_rewrite and might conflict with wordpress?
songdogtech
Both domain1 and domain2 are running two different Wordpress installations.
Liza
The easiest thing to do is use http://wordpress.org/extend/plugins/redirection/ rather than get into your .htaccess files.
songdogtech
A: 

Can't you just add to the header.php, before anything else:

<? php
if ($_GET['p'] == '123') {
    header('Location:http://www.yourotherdomain.com');
exit;
}

? I've done something similar in the past. It may not be pretty, but it works - I'd be interested to hear from more experienced php-ers whether it's a legitimate tactic.

TimS
Hi, I tried putting the code you gave into various parts of the header.php (above all code, above the <head> section, in the <head> section, but am getting this error (showing different lines depending on where I add it of course):Parse error: syntax error, unexpected T_IF in /home1/bluewidgets/public_html/foobar/wp-content/themes/scribblings/header.php on line 2I don't mind using this method at all instead of htaccess, just having some trouble getting it working.
Liza
Change the first line to <?php - notice no space and add ?> at the end and then try that.
Adam Dempsey
That worked perfectly, thanks! :DAnd thanks to all who helped.
Liza
A: 

Try this rule:

RewriteCond %{QUERY_STRING} =p=123
RewriteRule ^$ /foo/bar? [L,R=301]

Or using REQUEST_URI:

RewriteCond %{REQUEST_URI} =/?p=123
RewriteRule ^$ /foo/bar? [L,R=301]

Note that the RewriteCond patterns begin with a = that identifies a lexicographical comparison instead of a regular expressions test. Additionally the empty query in substitution (denoted with …?) that will remove the originally requested query.

Gumbo