See if this SO article helps: https://stackoverflow.com/questions/22287649/add-url-extensions-to-laravel-routes
Redirect "legacy" .html and .php extensions in nginx
This is something I run into quite frequently: Say a client has previously had a site with URIs like "about.php" or "contact.html". Now they have migrated to Laravel/Forge and the analogous URIs are simply "about" or "contact". However, search engine indices, print materials, etc. still list those old URLs with file extensions.
What is the most elegant solution to perform 301 redirects where appropriate, and to avoid 404 errors? i.e. 301 redirect "about.php" to "about" if "about" exists, or 404 if "about" does not exist.
Bonus points: also the best way to remap URLs whose names have changed from, say, "our-locations.html" to "locations"?
In apache:
Redirect 301 /oldpage.html http://www.yoursite.com/correctpage
In Nginx:
location /oldpage.html {
rewrite ^/.* http://$server_name/correctpage permanent;
}
Yes it's a pain to do with a lot of urls, but it keeps it from even hitting laravel and consuming resources. And you just have to do it once. Your scenario of "checking if it exists" could be problematic, unless you have individual routes for everything where you could check if the route existed.
Please or to participate in this conversation.