Link to home
Start Free TrialLog in
Avatar of jdvernier74
jdvernier74

asked on

Stripping multiple query strings in htaccess

My Google-fu is failing me on this issue, so I've come here to ask the gurus.

I recently moved a site to Wordpress and am trying to redirect some URL query strings that are no longer in use. However, my .htaccess skills are intermediate at best, and some of the things I'm trying to do are just not working. My Google rank is starting to slip because I can't get my redirects together. Please help!

Situation #1 - I used to have a page called searchartist.php that had two different query strings appended to the url: artist and album. Both query strings may have special characters in them such as commas, periods, parentheses, ampersands. A sample url might look like this: searchartist.php?artist=The+Beatles&album=Abbey+Road

I no longer have the searchartist.page. The new page is called playlist.php. What I want to do is take any request for the old searchartist.php page, strip both sets of query strings out completely, and redirect the user to the new playlist.php page.

To make this happen, I tried setting up something like this:

RewriteCond %{REQUEST_URI}  ^/searchalbum\.php$
RewriteCond %{QUERY_STRING} ^album=([a-zA-Z0-9+/\%\&\.\(\)]*)$
RewriteCond %{QUERY_STRING} ^album=([a-zA-Z0-9+/\%\&\.\(\)]*)$
RewriteRule ^(.*)$ http://www.sitename.com/playlist? [R=301,L]

This is not working at all. What am I doing wrong here?

2. I used to have a forum on this website (using phpbb3) but killed it in favor of a Wordpress blog. I want to take all requests for the old forum and send them to my blog page instead. To do this, I wrote:

RedirectMatch 301 ^/forum(.*) http://www.sitename.com/blog?

This works. The issue is that on redirect, the URL has the ? at the end of it in the address bar. What do I need to do to strip this?

Thanks in advance!
John
Avatar of xterm
xterm

1.  This single line will catch yoursite/searchalbum.php?<whatever> and take it to http;//www.sitename.com/playlist/ - all GET query strings will be discarded.

RewriteRule ^/searchalbum.php(.*) http://www.sitename.com/playlist/ [L,R]

2.  You have a trailing ? on the destination side of your redirect.   Just do:

RewriteRule ^/forum(.*) http://www.sitename.com/blog/ [L, R]



Enjoy :)
Avatar of jdvernier74

ASKER

Hi xterm,

Thanks for the answer.

When I tried #1, it doesn't work for me. I copied and pasted the line verbatim (changing sitename to my domain) and the redirect doesn't kick in. I get a 404 page as the server apparently disregards the directive and still tries to load searchartist.php?artist=Band+Name&album=Album+Name

When I tried #2, my server threw a 500 error.

Any thoughts? As I mentioned, I know enough htaccess to be dangerous but not enough to know what I'm doing and I'm completely stuck.
For number 1, I guess I typed that wrong, seeing searchalbum.php in your old rule, I fixed that to be searchartist.php

The htaccess file needs to be in the root directory of the website, and be sure you're actually turning the rewrite on with the RewriteEngine On directive.  Did you have this prior?  And is the .htaccess in the server root?

Do you have anything else in htaccess file other than what I've listed below.



RewriteEngine On
RewriteRule ^/searchartist.php(.*) http://www.sitename.com/playlist/ [L,R]
RewriteRule ^/forum(.*) http://www.sitename.com/blog/ [L, R]

Open in new window

Hi xterm,

Thanks again for your assistance.

Oops, my bad. That was my typo, not yours. I meant searchalbum.php.

As to your second question, let me address it one part at a time.
1. RewriteEngine On is in place. It's the first line in htaccess. I had this prior as I have other Rewrite Rules in place for other redirects (which are working).
2. .htaccess is in server root.
3. Yes - as mentioned in (1), I have other redirects in place. I have the following:

RewriteEngine On
RewriteBase /
ServerSignature Off

RedirectMatch 301 ^/forum(.*) http://www.sitename.com/blog?
RedirectMatch 301 ^/wp/(.*) http://www.sitename.com/blog?

RewriteRule ^/searchalbum.php(.*) http://www.sitename.com/playlist/ [L,R]

RedirectMatch 301 ^/blog/events/(.*) http://www.sitename.com/events?

RewriteCond %{REQUEST_URI}  ^/library\.php$
RewriteCond %{QUERY_STRING} ^artist=([a-zA-Z0-9+]*)$
RewriteRule ^(.*)$ http://www.sitename.com/playlist? [R=301,L]

RewriteCond %{REQUEST_URI}  ^/searchartist\.php$
RewriteCond %{QUERY_STRING} ^artist=([a-zA-Z0-9+]*)$
RewriteRule ^(.*)$ http://www.sitename.com/playlist? [R=301,L]

RewriteCond %{REQUEST_URI}  ^/songinfo\.php$
RewriteCond %{QUERY_STRING} ^songid=([0-9]*)$
RewriteRule ^(.*)$ http://www.sitename.com/playlist? [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

along with 20-30 different redirect 301 lines that target specific pages that had URL changes when I moved to wordpress.
Okay, for the 2 below, just remove the "?" off the end of both:

RedirectMatch 301 ^/forum(.*) http://www.sitename.com/blog?
RedirectMatch 301 ^/wp/(.*) http://www.sitename.com/blog?

That will fix #2.  I will test #1 on one of my own servers real quick and get back to you.
I really appreciate all the help xterm!

I changed both of the RedirectMatch 301 directives as requested. They both work with one problem: the query string is not getting stripped. So if I have something like /forum/index.php?p=1&r=2&t=3, it's making the redirect with the query string (/blog?p=1&r=2&t=3).i'd like to strip the query string out completely if it's possible.

Thanks again for helping me with this!

John
I'll get both working on one of my own machines today and get  you the confirmed working versions in the next few hours.
ASKER CERTIFIED SOLUTION
Avatar of xterm
xterm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason C. Levine
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.