0

I have some predefined links that I need to redirect to real URLs within my WP install. I have NO CHOICE but to use these URLs provided to me, unfortunately. The format is:

http://mysite.com/redirect.html?p=pagename

WP is installed at the root of mysite.com. I need to take the pagename query var, which will not be a direct match to a page URL and redirect it (301) to a WP permalink. I have attempted a few things in .htaccess with rewrites but not having luck mostly due to the fact that there are also WP permalinks redirecting.

Anyone done this before or know the best approach?

UPDATE - Here is the working code that I used based on the accepted answer below:

RewriteCond %{REQUEST_URI} ^/redirect.html(.*)$  
RewriteCond %{QUERY_STRING} ^(.*?)&?p=page&?(.*?)$  
RewriteRule (.*) /page/?%1&%2 [L,R=301]

I also added these before the base WP rewrites.

2
  • Please provide an example of what you have tried so far with your .htaccess file. Jan 23, 2014 at 18:21
  • Tried this way. I could have written this wrong as well, not an expert with mod rewrite. RewriteRule ^redirect\.html$ /perma-link [R=301] Jan 23, 2014 at 19:25

1 Answer 1

3

You could add something like this in your htaccess:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^redirect\.html$ /%1 [L,R=301]

EDIT: based on comments, you'll need some additional conditionals to sort different query strings.

RewriteCond %{REQUEST_URI} ^/redirect.html(.*)$
RewriteCond %{QUERY_STRING} ^p=test$
RewriteRule (.*) /new-test/ [L,R=301]

RewriteCond %{REQUEST_URI} ^/redirect.html(.*)$
RewriteCond %{QUERY_STRING} ^p=pagename$
RewriteRule (.*) /new-page/ [L,R=301]

I also highly recommend this site for testing htaccess: http://htaccess.madewithlove.be/

2
  • This is going where I need to go, but there may be more query parameters as well and those will need to be transferred to the redirect page. Jan 23, 2014 at 20:39
  • Got what I needed. Updated OP to reflect code. Jan 24, 2014 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.