301 Redirecting from old page to new page on Apache not working

13,036

Add question mark ? at the end of URL to prevent existing query string to be copied to a new URL:

Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage?

But since you are already using mod_rewrite, I would recommend utilising it for this task as well (place this rule above your other rewrite rules):

RewriteRule ^oldpage$ http://www.mysite.co.uk/newsubdir/newpage? [R=301,L]
Share:
13,036
zigojacko
Author by

zigojacko

Founder and CEO of the The Clubnet Group and Director of Search & Operations at Clubnet Digital, a UK (Plymouth) based full service digital agency. We help businesses perform better on the web.

Updated on June 04, 2022

Comments

  • zigojacko
    zigojacko almost 2 years

    A simple 301 redirect is not working in this instance - For example:

    Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage
    

    The site is dynamic and the .htaccess is already renaming pages to search engine friendly URL's from URL's containing a query string.

    RewriteRule ^(.*)/(.*)$  index.php?page_name=$1&sub=$2 [NC,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)([^/])$ http://www.mysite.co.uk/$1$2/ [R=301,L]
    

    When we are using these 301 redirects like above in the same .htaccess (at the bottom), the pages are redirecting but the query string is getting added to the end of the URL frustratingly and we have not figured out why or how to prevent it.

    After 301 redirect, URL is looking like:-

    http://www.mysite.co.uk/newsubdir/newpage/?page_name=old-page&sub=
    

    ...Causing a 404 error - it's just the query string added on the end of the URL's that is breaking the redirect.

    Please can anyone advise on what needs to be done to fix this?

    Thanks