1

As the title says, content is being returned correctly but the status code is 404.

I have checked the .htaccess file and the Apache config file as well. All seems to be fine.

I have the following in my module routing file:

my_module.news_list:
  path: /api/{state}/news
  defaults:
    _controller: Drupal\my_module\Controller\NewsController::index
  methods: [GET]
  requirements:
    _access: 'TRUE'

This is the function that does the redirect in my EventSubscriber

public function redirectRequestToModule(GetResponseEvent $event) {
            $request = $event->getRequest();
            $validPathBegins = ['/men', '/nat'];
            // This is necessary because this also gets called on
            // node sub-tabs such as "edit", "revisions", etc.  This
            // prevents those pages from being redirected.
            $requestUri = $request->getRequestUri();
            if (!in_array(substr($requestUri, 0, 4), $validPathBegins)) {
                return;
            }
            
            $node = $request->attributes->get('node');
            
            // get the configured request query parameters for this content type
            
            $redirect_url = '/api' . $requestUri;
            $kernel = $event->getKernel();
            $response = $kernel->handle(Request::create($redirect_url));
            $event->setResponse($response);
        }

This is what I type in the browser: mysite.com/nat/news/

The content returned is correct but the status code is 404.

P.S There isn't a reachable path on my Drupal website like /nat/news. The path mentioned is supposed to return a list of news articles.

4
  • This is a bit confusing what you want to achieve. There are different concepts, redirecting, subrequesting and aliasing. The latter is what I would prefer in this case, which is not only possible through the core alias table, but also through a custom path processor converting the external path /nat/news to an internal path matching a route.
    – 4uk4
    Sep 12, 2020 at 16:12
  • Thanks. Creating an alias in /admin/config/search/path/add worked. I created an alias /api/nat/news/ to /nat/news
    – hirani89
    Sep 13, 2020 at 4:53
  • If you have a large number of aliases all with the same pattern you can do this programmatically, see drupal.stackexchange.com/a/187497/47547
    – 4uk4
    Sep 13, 2020 at 7:07
  • Thanks a lot, that makes it much easier.
    – hirani89
    Sep 14, 2020 at 12:38

1 Answer 1

2

Creating an alias in /admin/config/search/path/add worked. I created an alias /api/nat/news/ to /nat/news

Thanks 4k4 for a much easier solution drupal.stackexchange.com/a/187497/47547

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.