You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel路由添加例外:如何配置WordPress博客专属路由?

How to Add a WordPress Blog Route to Your Laravel App (CentOS 7 + Apache)

Hey there! Let's break this down step by step—since your existing Laravel route /{en}/{county} is a catch-all for two-segment paths, we need to make sure the new /{en}/blog route takes priority, then route those requests over to your WordPress installation. Here's what to do:

1. Update Laravel Routes to Prioritize the Blog Route

First, open your routes/web.php file. We need to add the blog route before your existing /{en}/{county} route—this way Laravel matches the blog path first instead of treating "blog" as a county parameter.

Add this route at the top of the file:

// Match any language code + /blog (and any subpaths under blog)
Route::get('{en}/blog/{any?}', function ($en, $any = null) {
    // Point to your WordPress index.php (adjust the path if your WP is elsewhere)
    $wpIndex = public_path('blog/index.php');

    // Update server variables so WordPress recognizes the correct URL structure
    $_SERVER['REQUEST_URI'] = "/{$en}/blog/" . ($any ?? '');
    $_SERVER['SCRIPT_NAME'] = "/{$en}/blog/index.php";

    // Pass the request to WordPress
    require $wpIndex;
    exit;
})
->where('en', '[a-z]{2}') // Restrict to 2-letter language codes (adjust if needed)
->where('any', '.*'); // Match any subpath under /blog

// Keep your existing route below this one
Route::get('{en}/{county}', function ($en, $county) {
    // Your existing route logic here
})
->where('en', '[a-z]{2}')
->where('county', '[a-zA-Z0-9-]+');

2. Configure Apache .htaccess Files

Laravel Root Directory .htaccess

Open the .htaccess file in your Laravel project root (not the public folder) and add a rewrite rule to forward blog requests to the WordPress directory before Laravel's default rewrites:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Forward /{en}/blog requests to the public/blog directory
    RewriteRule ^([a-z]{2})/blog(.*)$ public/blog$2 [L]

    # Laravel's default rewrite rules
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

WordPress Directory .htaccess

If your WordPress is in public/blog, create or update its .htaccess to handle the language-prefixed URL:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Skip index.php itself
    RewriteRule ^index\.php$ - [L]

    # Strip the language code prefix from the request for WordPress
    RewriteRule ^([a-z]{2})/blog/(.*)$ $2 [L]

    # Handle WordPress permalinks
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

3. Set Up WordPress Site URLs

Log into your WordPress admin dashboard, go to Settings > General, and update these fields:

  • WordPress Address (URL): http://example.com/public/blog (use your actual domain and WP directory path)
  • Site Address (URL): http://example.com/en/blog (replace en with your default language code—if you need multi-language blogs, use a plugin like Polylang to manage language-specific paths)

4. Verify Apache Configuration

Make sure Apache has the required modules enabled and allows .htaccess overrides:

  1. Enable mod_rewrite and mod_negotiation:
    sudo a2enmod rewrite
    sudo a2enmod negotiation
    sudo systemctl restart httpd
    
  2. Check your Apache virtual host config (usually in /etc/httpd/conf.d/your-site.conf) to ensure the Laravel public directory allows overrides:
    <Directory /var/www/html/your-laravel-project/public>
        AllowOverride All
        Require all granted
    </Directory>
    
    Restart Apache again after making changes:
    sudo systemctl restart httpd
    

That should do it! Now when you visit http://example.com/en/blog (or any other language code you use), it'll load your WordPress blog, while your existing /{en}/{county} routes still work as expected.

内容的提问来源于stack exchange,提问作者S.M_Emamian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:43:15