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

RewriteRule messing up 301 redirects


I am in the process of rebuilding my entire website, the previous version used only static html pages and was a nightmare to manage.

In the new site I already have a .htaccess with the following:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /MySite/?q=$1

It rewrites URLs via PHP.

However, what I also need to do is add lines like this:

Redirect 301 /oldpage.html http://www.mysite.com/dir/newpage.html
Redirect 301 /oldpage2.html http://www.mysite.com/dir/newpage2.html
Redirect 301 /oldpage3.html http://www.mysite.com/dir/newpage3.html

The URL directory structure of my site has completely changed, I have a lot of pages indexed in google and need to 301 them to the new URL.

I've tried adding these new 301 directives to the end of my existing .htaccess but then I end up being redirected like this:

http://www.mysite.com/dir/newpage.html?q=oldpage.html

I don't want it to have the query string on the end, how can I remove it?


Solution

  • Just put them before the main redirect with L directive:

    Options +FollowSymlinks
    RewriteEngine on
    
    RewriteRule ^oldpage\.html$ /dir/newpage.html [R=301,NC,L]
    RewriteRule ^oldpage2\.html$ /dir/newpage2.html [R=301,NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /MySite/?q=$1