Paul N.
Paul N.

Reputation: 25

How to rediect in htaccess .php file with many IDs

I have many old URLs that I deleted:

viewnews.php?id=1357570020
viewnews.php?id=1565656889
viewnews.php?id=7877878768
viewnews.php?id=7876876876

I have like a thousand URLs like that. I want all those URLs redirected to my main page. They do not exist anymore.

I want when someone goes to mysite.com/viewnews.php?id=(ANY ID HERE) to automatically forward to www.mysite.com

How do I do that in htaccess? I have tried many things.

Redirect 301 /viewnews.php(.*)$ http://www.mysite.com/

Any help would be greatly appreciated.

Thanks.

Upvotes: 1

Views: 85

Answers (2)

anubhava
anubhava

Reputation: 785146

Using mod_rewrite you can use following code to handle this task:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+viewnews\.php\?id=[^\s]+ [NC]
RewriteRule ^ http://%{HTTP_HOST}? [R=301,L]

Upvotes: 1

seangates
seangates

Reputation: 1522

Use RedirectMatch instead. Reference this site: http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

If you need to mass-redirect a group of pages you would probably need to use the RedirectMatch directive.

Therefore, you use that directive and you need to tell the regex where to start "^" and where to end the regex "$". Try this:

RedirectMatch ^/viewnews.php?id=([0-9]*)$ http://www.mysite.com/

Take two pills, and call me in the morning. :D

Upvotes: 0

Related Questions