0

How to redirect page to specific url suppose blog-landing page to blog-landing/all page

How to target specific page /blog-landing page when user enters url into browser user should redirect to blog-landing/all

What i have tried :

 function bootstrap_preprocess_page(&$variables) {

 $request = \Drupal::request();
 $current_path = $request->getPathInfo();
 $path_args = explode('/', $current_path);
 $first_argument = $path_args[1];
  if($first_argument =="blog-landing")
  {
    $response = new Symfony\Component\HttpFoundation\RedirectResponse('/node/12/edit');
 $response->send(); 
return;

}

else
{

 echo "sorry not redirected";
 }
}
}
0

Use a default parameter (for example blog_id) in your routing.yml file:

mymodule.route_name:
  path: '/blog-landing/{blog_id}'
  defaults:
    _title: 'Show blogs.'
    _controller: '\Drupal\mymodule\Controller\BlogController::viewAction'
    blog_id: 'all'

If the blog_id parameter is omitted the default value ('all') is used.

After your edit:

If you are redirecting via the RedirectResponse() class you should call the send() method in order to redirect.

  • Tx i have done changes in my code after your suggestion – user59414 Jun 6 '17 at 10:40
  • can you suggest on more changes in my code why its not creating complete path /node/12/edit – user59414 Jun 6 '17 at 10:45
  • I'm afraid I don't understand your question. The /node/{node}/edit route is a core route: entity.node.edit_form => /node/{node}/edit. For redirecting to a node edit page please see drupal.stackexchange.com/questions/190376/… – mvdgun Jun 6 '17 at 10:59
0

One other way, without using additional modules or rewrite rules would be to create a content with the alias oldurl adding the following full html content to the body:

<script>
window.location="http://www.yoursite.com/newurl"
</script>
  • Like I said, create a content (of any content type, ex: page), on the body field, select the format full html, put the above JavaScript code. Create an alias of with the url you want to be redirect so that the path to that content becomes something yoursite.com/oldurl, and save the content. – Anibal Santos Jul 26 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy