PHP code example of ozkanozcan / laravel-404-to-301

1. Go to this page and download the library: Download ozkanozcan/laravel-404-to-301 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ozkanozcan / laravel-404-to-301 example snippets


return [
    // Enable or disable the redirect middleware globally
    'enabled' => env('REDIRECT_404_ENABLED', true),

    // Log missing 404 URLs to Laravel Log files
    'log_missing' => env('REDIRECT_404_LOG_MISSING', true),
    'log_channel' => env('REDIRECT_404_LOG_CHANNEL', 'daily'),

    // Save missing 404 URLs to the `missing_urls` database table
    'db_log_missing' => env('REDIRECT_404_DB_LOG', true),

    // Default status code (301 for permanent, 302 for temporary)
    'default_code' => env('REDIRECT_404_DEFAULT_CODE', 301),

    // Ignore patterns to exclude static assets
    'ignore_patterns' => [
        '*.css', '*.js', '*.ico', '*.png', '*.jpg', '*.jpeg',
        '*.gif', '*.svg', '*.webp', '*.woff2', '/favicon*', '/robots.txt',
    ],

    // Cache TTL in seconds (0 = disabled)
    'cache_ttl' => env('REDIRECT_404_CACHE_TTL', 3600),
];

use OzkanOzcan\Laravel404To301\Http\Middleware\Handle404Redirect;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            Handle404Redirect::class,
        ]);
    })
    ->create();

protected $middlewareGroups = [
    'web' => [
        // ...
        \OzkanOzcan\Laravel404To301\Http\Middleware\Handle404Redirect::class,
    ],
];

use OzkanOzcan\Laravel404To301\Models\Redirect;
use OzkanOzcan\Laravel404To301\Redirect404Facade as Redirect404;

// Add a new redirect rule
Redirect::create([
    'from_url'      => '/old-services',
    'to_url'        => '/services',
    'redirect_code' => 301,
    'is_active'     => true,
]);

// Flush cache after updating rules manually
Redirect404::flushCache();
bash
php artisan vendor:publish --tag=redirect404-config
php artisan vendor:publish --tag=redirect404-migrations
bash
php artisan migrate
bash
php artisan redirect:missing --limit=20 --sort=hits
bash
php artisan redirect:prune-missing --days=30