Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Stripping multiple query strings in htaccess

Posted on 2011-10-30
10
Medium Priority
?
314 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:jdvernier74
  • 5
  • 3
9 Comments
 
LVL 19

Expert Comment

by:xterm
ID: 37054972
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 :)
0
 

Author Comment

by:jdvernier74
ID: 37055028
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.
0
 
LVL 19

Expert Comment

by:xterm
ID: 37055042
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jdvernier74
ID: 37055052
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.
0
 
LVL 19

Expert Comment

by:xterm
ID: 37055067
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.
0
 

Author Comment

by:jdvernier74
ID: 37056377
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
0
 
LVL 19

Expert Comment

by:xterm
ID: 37056816
I'll get both working on one of my own machines today and get  you the confirmed working versions in the next few hours.
0
 
LVL 19

Accepted Solution

by:
xterm earned 2000 total points
ID: 37057636
Okay, these both work on my server:

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

As it turns out, the trailing "?" character is what tells it to discard the query string - not sure how I forgot that.

0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 37343896
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wordpress Horizontal Drop-Down Menu In this tutorial I will show you had to add a WordPress horizontal navigation menu to your theme. I have searched and searched for a good tutorial on creating a WordPress nav menu without adding a plug-in or us…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question