Search code examples
.htaccesshttp-redirecthttp-status-code-301

Cut off parameter from redirect in htaccess


This silent redirect in htaccess:

RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]

redirect a url that looks like:

example.com/album_showpage.php?pic_id=1906

to:

example.com/gallery/image_page.php?image_id=1906

that works great. But when the URL has a parameter like:

album_showpage.php?pic_id=1906&mode=prev

or

album_showpage.php?pic_id=1906&mode=next

the redirect wont work.

Question: How to cut of any parameter after pic_id=1906

thank you


Solution

  • You need match against the rest of the query string.

    RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
    RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
    

    If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:

    RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
    RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]