skip to Main Content

Basically I’d like to do a 301 re-direct from one specific dynamic page that has a question mark character (?) in the URL to a static page.

The reason I want to do this is that this page is effective for SEO, but it will benefit from its own page now. I’ve used Rewrite rules for redirecting query strings many times, but I can’t seem to figure how to escape, say, a specific url that has a ?. For example:

Redirect 301 /page.php?page_id=1 /new-page/
Redirect 301 /page.php?page_id=10 /cool-product/

I do not need to do the following:

RewriteRule ^page/([0-9]+)/([a-zA-Z0-9_-]+)/$ ./page.php?page_id=$1 [R=301,L]

3

Answers


  1. You can make rules that are run against the query string using mod_rewrite like:

    RewriteCond %{REQUEST_URI}  ^/page.php$
    RewriteCond %{QUERY_STRING} ^page_id=1$
    

    Here is a decent write up on the topic: https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/

    Login or Signup to reply.
  2. You can not redirect url with querystrings to a static page using Redirect directive, you need to use mod-rewrite ,so try this :

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} /page.php?page_id=1 [NC]
    RewriteRule ^ /newpage/? [L,R]
    RewriteCond %{THE_REQUEST} /page.php?page_id=10 [NC]
    RewriteRule ^ /anotherpage/? [L,R]
    

    Empty question mark at the end of the target url is important as it discards the old query strins from the new url.

    Login or Signup to reply.
  3. Strips all Query Strings that start with do= ONLY if the Request URI is classifieds/ and redirects to the /ads/ URI: http://www.example.com/classifieds/?do=detailpage&cat=3&id=14 to http://www.example.com/ads/

    # Redirects ONLY if the URI is classifieds/ and Strips the do= Query Strings
    # from the destination /ads/ URL|URI
    RewriteCond %{QUERY_STRING} ^do=(.*)$ [NC]
    RewriteRule ^classifieds/$ http://www.example.com/ads/$1? [R=301,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search