0

I want to redirect old url(https://xklsv.me/viewblog.php?title=plant-trees%2Faranya_kfd%2Fplant-trees%2FSeptember-14th-2019) into new url (https://xklsv.me/plant-trees/aranya_kfd/September-14th-2019) for a website(Permanent Redirect 301).

There are more than 10,000 pages in this case.

I can not do a RewriteRule for each existing page. Is there a way to perform a RewriteRule using a regular expression?

For example: I want to redirect an URL like https://xklsv.me/viewblog.php?title=plant-trees/aranya_kfd/plant-trees/September-14th-2019 to https://xklsv.me/plant-trees/aranya_kfd/September-14th-2019

  • 2
    Possible duplicate of [How do I make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php) – LF00 Sep 17 '19 at 08:10
  • @KrisRoofe RewriteRules are from apache, not php. The question might not be clear enough but it's not about making a redirect in php. – jeprubio Sep 17 '19 at 08:38
  • my all the old url is not clean url but new urls are clean url so i want to redirect all the old url into clean url – Prakash yadav Sep 17 '19 at 09:34

2 Answers2

0

This RewriteRule should work:

RewriteCond %{QUERY_STRING} ^title=([^/]+)/([^/]+)/([^/]+)/(.+)$
RewriteRule ^viewblog\.php$ /%1/%2/%4? [R=301]

this regex is splitting all the url parts after the title argument with the regex and then ignoring the 3rd match as plant-trees is not repeated in the new url. This will do a redirect on your browser.

And then use this one to call the viewblog.php internally

 RewriteRule ^([^/]+)/([^/]+)/(.+)$ viewblog.php?title=$1/$2/$1/$3 [L]

Then you just have to update the links on the source code of your website and wait for the crawlers to update them.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • this is not working it shows 404 not found error when i click any link – Prakash yadav Sep 17 '19 at 09:31
  • By the way, when I click on the link you provided as example a 404 is shown. I think you want to do the opposite, to redirect every call at `https://xklsv.me/plant-trees/aranya_kfd/September-14th-2019` to `https://xklsv.me/viewblog.php?title=plant-trees/aranya_kfd/plant-trees/September-14th-2019` – jeprubio Sep 17 '19 at 10:55
  • i have working code RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ viewblog.php?title=$1&user=$2&date=$3 [L] but when i search same link on google so its giving this url https://xklsv.me/viewblog.php?title=plant-trees/aranya_kfd/plant-trees/September-14th-2019 – Prakash yadav Sep 17 '19 at 11:00
  • i want like this url https://xklsv.me/plant-trees/aranya_kfd/September-14th-2019 in case of google search also – Prakash yadav Sep 17 '19 at 11:11
  • now this https://xklsv.me/plant-trees/aranya_kfd/September-14th-2019 url works with that rewrite rule, you should update the links at your website's source and wait until the links at google search are updated when the crawler inspects your source – jeprubio Sep 17 '19 at 11:13
  • i have updated the links at my website's source still not coming in the google search check with this keyword xklsv.me aranya_kfd plant trees – Prakash yadav Sep 17 '19 at 11:22
  • of course, this will take several days, until the google crawler updates your website the links will not be updated – jeprubio Sep 17 '19 at 11:23
0

This should work:

^\/viewblog\.php\?[^\/]+\/([^\/]+)\/([^\/]+)\/(.+)$ /$1/$2/$3 [R=301,L]

this will match 3 groups in your url and redirect to them.

muszkin
  • 1
  • 5