0

I am making a new website to replace an existing website. And we are not keeping the previous permalink structure : see below
OLD URL = http www .domain.com/article.php?ID=3242
NEW URL = http www .domain.com/author/post-name

So we want to redirect the old urls to the new ones. How could I do that easily in the htaccess file ?

I tried redirect 301 but does not work.
RedirectMatch 301 ^/article.php?ID=3242 http://www.domain.com/author/post-name

Miguel Bocquier
  • 2,449
  • 5
  • 22
  • 38

1 Answers1

1

RedirectMatch won't match the query string (the part after the ?). This article explains a bit more, but this should work:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^ID=3242$
RewriteRule ^(.*)$ http://www.domain.com/author/post-name [R=302,L]

Note that I changed it to a 302 redirect while you're testing; I'd only change it to a 301 once you're sure you've got it right (to avoid browsers caching incorrect redirects).

Hobo
  • 7,536
  • 5
  • 40
  • 50
  • Ouah cool. But where should I put it : before/after the rules of wordpress ? # BEGIN WordPress RewriteEngine On RewriteBase /withoutmodel/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /withoutmodel/index.php [L] # END WordPress – Miguel Bocquier May 28 '13 at 17:32
  • I'd put it before the WordPress rules; straight after ``. That'll mean you have two `RewriteEngine On` directives - you could remove the second one. – Hobo May 29 '13 at 06:13
  • Can you post the contents of your `.htaccess` file (just edit your question)? – Hobo Jun 02 '13 at 12:32
  • Works great now ! Thank you so much. For info I put the lines before the modrewrite rules of WORDPRESS. And for localhost dev where I got subdir for my projects I had to remove the ^ rule here : RewriteCond %{REQUEST_URI} /article\.php$ – Miguel Bocquier Jun 13 '13 at 10:04
  • Just one detail more : is there a way to remove the querystring in the redirected url ? Because the querystring is stil here in the final page : ...domain.com/author/post-name/?ID=3242 – Miguel Bocquier Jun 13 '13 at 10:08
  • Excellent; glad to help. This question has an approach: http://stackoverflow.com/questions/16156301/htaccess-remove-query-string – Hobo Jun 13 '13 at 11:24