Question

Everything I can find for query string redirects talks about something like this:

RewriteEngine on 
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]

That's great, but I'm dealing with a url that looks like this:

example.com/portfolio/?gal=16

If I test it without the "portfolio/" it will work fine, but my old site is indexed as above. Any tips on how I can make it work with my URL structure?

EDIT Current .htaccess contents

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# Redirects

Redirect 301 /feedback http://example.com/testimonials
Redirect 301 /about http://example.com/about-us
Redirect 301 /contact http://example.com/contact-us

#futile attempts at query string redirects omitted

Thanks.

New Edit to include @anubhava's suggestions

Might as well give you the actual .htaccess content since the site is now live.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^dev.bayshorephotography.com$ [OR]
RewriteRule (.*)$ http://bayshorephotography.com/$1 [R=301,L]

# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.bayshorephotography.com$ [NC]
RewriteRule ^(.*)$ http://bayshorephotography.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^gal=16$
RewriteRule .? http://bayshorephotography.com/portfolio/weddings-02/? [L,R=301]

Redirect 301 /feedback http://bayshorephotography.com/testimonials
Redirect 301 /about http://bayshorephotography.com/about-us
Redirect 301 /contact http://bayshorephotography.com/contact-us

#301 Redirect Entire Directory to remove /blog/ from all blog pages except the main archive itself
RedirectMatch 301 /blog(/.*)/ $1

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

# END WordPress

# Old Redirects that don't work
#Redirect 301 /portfolio?gal=20 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=29 http://bayshorephotography.com/portfolio-items/our-world-03/
#Redirect 301 /portfolio?gal=16 http://bayshorephotography.com/portfolio-items/weddings-02/
#Redirect 301 /portfolio?gal=18 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=28 http://bayshorephotography.com/portfolio-items/album-samples-01/
#Redirect 301 /portfolio?gal=19 http://bayshorephotography.com/portfolio-items/weddings-05/
#Redirect 301 /portfolio?gal=22 http://bayshorephotography.com/portfolio-items/portraits-01/
#Redirect 301 /portfolio?gal=24 http://bayshorephotography.com/portfolio-items/our-world-01/
#Redirect 301 /portfolio?gal=17 http://bayshorephotography.com/portfolio-items/weddings-03/
#Redirect 301 /portfolio?gal=36 http://bayshorephotography.com/portfolio-items/portraits-04/
#Redirect 301 /portfolio?gal=35 http://bayshorephotography.com/portfolio-items/portraits-03/
#Redirect 301 /portfolio?gal=32 http://bayshorephotography.com/portfolio-items/weddings-01/
#Redirect 301 /portfolio?gal=25 http://bayshorephotography.com/portfolio-items/our-world-02/
#Redirect 301 /portfolio?gal=23 http://bayshorephotography.com/portfolio-items/portraits-02/
#Redirect gone /portfolio?gal=34
#Redirect gone /portfolio?gal=33
#Redirect gone /portfolio?gal=27
#Redirect gone /portfolio?gal=21
Was it helpful?

Solution

You need to change order of your rules and keep 301/302 before WP rules:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://example.com/? [L,R=301]

# Redirects
RewriteRule ^feedback/?$ http://example.com/testimonials [L,R=301]
RewriteRule ^about/?$ http://example.com/about-us [L,R=301]
RewriteRule ^contact/?$ http://example.com/contact-us [L,R=301]

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

# END WordPress
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top