.htaccess file redirect with parameter

Passing the user to new pages

At the moment I am completely re-designing and re-structuring my current web pages. Along the way there is random problem that needs to be solved. In this case old blog posts by a wordpress solution has url's to a totally different path of the server. Fixing this problem I needs to do 404 redirect with a url-path as a parameter. 

Passing URL for re-directs on the HTACCESS file

1. Type this into the .htaccess file 

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) https://www.path-to-url/blog-path/post-redirect.php?q=$1 [R=301,L]


The code snippet above is for the .htaccess file and this will redirect any broken links with a paramter to an custom made php file where you can easily customize the path or extra adjustment. See below for an example,

2. Customize php script to handle the parameter

echo"Post redirect ";
// Remove the last char since it is a slash "/"
$path = substr(htmlspecialchars($_GET["q"]), 0, -1);
// If the string path has the word 'blog/' within it - remove it since then it is a category search page.
if (strpos($path, 'blog/') !== false) {
$path = str_replace('blog/', '', $path) ;
header("Location: https://www.path-to-url/category-page.php?post=".$path);
//Pass the main parameter to the new blog post path
header("Location: https://www.path-to-url/blog-path.php?post=".$path);