redirect with multiple query

146 views Asked by At

I've been searching for hours now but cannot find the working solution for this problem. I'm redesigning a website and need to redirect a bunch of old url's to new url's. The basic format is something like:pages/page.php?m=22&p=33should go to something like mainpage/subpage/ while completely ignoring all parameters. After having tried multiple variations I currently have this (as part of a Wordpress site) in my htaccess. The problem arises when having 2 parameters in the url to redirect instead of 1 or none.

# REDIRECTS FROM OLD WEBSITE
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# custom redirects
Options +FollowSymLinks -MultiViews
RewriteCond %{QUERY_STRING} ^m=([0-9]+)&p=([0-9]+)$ [nc]
RewriteRule ^pages/all_inclusive_zeildag\.php$ /bedrijfsuitjes/volledig\-verzorgd\-zeilen/ [R=301,NC,L]
</IfModule>
# END REDIRECTS OLD WEBSITE

# BEGIN WordPress
all the usual wp code
#
# END WordPress

I have almost got this working. Problem is the second parameter. Properly redirected are: pages/all_inclusive_zeildag.php?m=8 and if I redirect without any parameter at all.

But the problem is:/pages/all_inclusive_zeildag.php?m=8&p=22 which leads to a 404

Any insight are more than welcome.

Thanks, John

3

There are 3 answers

0
Magentron On

A "nice" way to see what is happening while processing the rewrite rules is using the RewriteLog and RewriteLogLevel directives in Apache 2.2 mod_rewrite:

RewriteEngine On
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 3

Or in Apache 2.4 mod_rewrite using the regular LogLevel directive:

LogLevel alert rewrite:trace3

See also: https://wiki.apache.org/httpd/RewriteLog, https://stackoverflow.com/a/9632952/832620

0
John On

Okay. Thanks for all the input. Somehow it got me on the right track and I ended up with


RewriteEngine On
#RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pages/all_inclusive_zeildag\.php /bedrijfsuitjes/volledig-verzorgd-zeilen/%1? [L,R=301,NC]
RewriteRule ^pages/avondopleiding\.php /leren-zeilen/avondles-zeilen/%1? [L,R=301,NC]

0
Dennis Docter On

The first two are not being picked up by this rewriterule, as expected.

The 3rd is actually doing exactly what you're asking it to do:

http://57755478.swh.strato-hosting.eu/pages/all_inclusive_zeildag.php?m=8&p=22

Results in a 301 towards: http://57755478.swh.strato-hosting.eu/bedrijfsuitjes/volledig-verzorgd-zeilen/?m=8&p=22

(Use devtools network tab, and turn on preserve logs if you're using chrome)

If you intended the query string to be stripped, then you'll need to append a question mark to the redirect url:

RewriteRule ^pages/all_inclusive_zeildag.php$ /bedrijfsuitjes/volledig-verzorgd-zeilen/? [R=301,NC,L]

Also for clarity, group the RewriteCond statements together with the rewrite rule, since they will only apply to the first RewriteRule following them.

Good luck!