zigojacko
zigojacko

Reputation: 2063

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

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

Upvotes: 3

Views: 15067

Answers (2)

Naishdh
Naishdh

Reputation: 11

redirect 301 /oldfile.htm http://www.example.com/newfile.htm

Use it. Working fine.

Upvotes: 1

LazyOne
LazyOne

Reputation: 165288

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]

Upvotes: 8

Related Questions