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

Ubuntu云服务器CakePHP子域名重定向至对应目录的技术求助

Subdomain Redirect to CakePHP Path on Ubuntu: Step-by-Step Guide

Hey there! Let’s get this subdomain redirect sorted out for your CakePHP site hosted on Ubuntu. Since you’ve worked with mod_proxy before, I’ll cover both Apache (the most common setup for CakePHP) and Nginx configurations—pick whichever matches your web server.


First: Prerequisites

Before diving in, make sure you’ve:

  • Pointed your subdomain (subdomain.mysite.com) to your Ubuntu server’s IP address via your DNS provider.
  • Enabled necessary Apache modules (if using Apache):
    sudo a2enmod rewrite proxy proxy_http
    sudo systemctl restart apache2
    

Option 1: Apache Setup (Using .htaccess or Virtual Hosts)

Method A: Simple .htaccess Redirect (Quickest)

Your CakePHP site’s root directory (usually /var/www/mysite/) should already have an .htaccess file for routing. Add these rules to it:

RewriteEngine On

# Redirect any subdomain (except www) to the corresponding path on www.mysite.com
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.mysite\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ https://www.mysite.com/%1/$1 [R=301,L]
  • What this does: Matches any subdomain (like blog.mysite.com), excludes www, and redirects it permanently (301) to www.mysite.com/blog/ (keeping any extra path after the subdomain).

Method B: Virtual Host Configuration (More Control)

If you prefer managing redirects at the Apache server level (better for multiple subdomains):

  1. Create a new virtual host file for your subdomain:
    sudo nano /etc/apache2/sites-available/subdomain.mysite.com.conf
    
  2. Add this content for HTTP (update paths if needed):
    <VirtualHost *:80>
        ServerName subdomain.mysite.com
        # Permanent redirect to the target path
        Redirect permanent / https://www.mysite.com/subdomain/
    </VirtualHost>
    
  3. For HTTPS (add if you have an SSL certificate, e.g., from Let’s Encrypt):
    <VirtualHost *:443>
        ServerName subdomain.mysite.com
        SSLEngine On
        SSLCertificateFile /etc/letsencrypt/live/mysite.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/mysite.com/chain.pem
    
        Redirect permanent / https://www.mysite.com/subdomain/
    </VirtualHost>
    
  4. Enable the virtual host and reload Apache:
    sudo a2ensite subdomain.mysite.com.conf
    sudo systemctl reload apache2
    

Option 2: Nginx Setup

If you’re using Nginx instead of Apache:

  1. Edit your site’s Nginx config file (usually /etc/nginx/sites-available/mysite.conf):
    sudo nano /etc/nginx/sites-available/mysite.conf
    
  2. Add these server blocks for your subdomain:
    # Redirect HTTP subdomain to HTTPS target path
    server {
        listen 80;
        listen [::]:80;
        server_name subdomain.mysite.com;
        return 301 https://www.mysite.com/subdomain$request_uri;
    }
    
    # Redirect HTTPS subdomain to target path
    server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name subdomain.mysite.com;
    
        ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
    
        return 301 https://www.mysite.com/subdomain$request_uri;
    }
    
  3. Enable the config and restart Nginx:
    sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
    sudo nginx -t # Test for syntax errors
    sudo systemctl restart nginx
    

Final CakePHP Routing Check

Make sure your CakePHP app can handle the /subdomain path! Open config/routes.php and add a route to map the subdomain path to your desired controller/action:

// Map /subdomain/* to your controller action
$routes->connect('/:subdomain/*', [
    'controller' => 'YourController',
    'action' => 'yourAction'
], ['pass' => ['subdomain']]);

This will pass the subdomain name as a parameter to your controller action, so you can customize behavior based on it.


Test the Redirect

Use curl to verify the redirect works without opening a browser:

curl -I https://subdomain.mysite.com

You should see a 301 Moved Permanently response pointing to https://www.mysite.com/subdomain/.

内容的提问来源于stack exchange,提问作者Pseudomax

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:58:03