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

如何在AWS AMI实例中移除URL中的index.php?美化EC2实例Laravel等框架URL

Hey there! Let's walk through how to strip index.php from your URLs and get clean, pretty routing for Laravel, CodeIgniter, or other frameworks on an Amazon AWS AMI EC2 instance. I'll split this up by web server (Apache vs. Nginx) since the steps differ a bit, and cover the most common frameworks.

1. First, Confirm Your Web Server

Amazon Linux AMIs often come with Apache pre-installed, but if you opted for Nginx, we'll cover both scenarios.


For Apache Users

Step 1: Enable mod_rewrite

Apache's rewrite module is what makes URL rewriting possible. Run these commands:

sudo a2enmod rewrite
sudo systemctl restart httpd  # Or `sudo service httpd restart` for older AMIs

Step 2: Allow .htaccess Overrides

Next, you need to let Apache respect .htaccess files in your framework's directory. Edit your Apache config file (usually /etc/httpd/conf/httpd.conf or a site-specific file in /etc/httpd/conf.d/):

  • Find the <Directory> block for your website's root folder
  • Change AllowOverride None to AllowOverride All
  • Save the file and restart Apache again with sudo systemctl restart httpd

Step 3: Configure .htaccess for Your Framework

Laravel

Laravel's public folder already has a default .htaccess, but double-check it looks like this (or create it if missing):

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Important: Make sure your Apache virtual host's DocumentRoot points directly to Laravel's public folder, not the root of the Laravel project. This is a common gotcha!

CodeIgniter

Create a .htaccess file in your CodeIgniter root directory with this content:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Remove index.php from URLs
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Then open application/config/config.php and update these settings:

$config['index_page'] = ''; // Remove 'index.php' here
$config['uri_protocol'] = 'REQUEST_URI'; // Ensures proper routing

For Nginx Users

Nginx doesn't use .htaccess, so we'll edit your site's Nginx config directly.

Step 1: Update Your Site Config

Find your site's config file (usually in /etc/nginx/conf.d/ or /etc/nginx/sites-available/).

Laravel

Ensure your server block points to the public folder and includes the try_files rule:

server {
    listen 80;
    server_name your-domain.com; # Or your EC2 public IP
    root /var/www/laravel/public;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # Use 127.0.0.1:9000 if using TCP instead of Unix socket
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

CodeIgniter

Set up your server block like this:

server {
    listen 80;
    server_name your-domain.com;
    root /var/www/codeigniter;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Just like with Apache, update CodeIgniter's config.php to set index_page to empty and uri_protocol to REQUEST_URI.

Step 2: Restart Nginx

Apply the changes with:

sudo systemctl restart nginx

2. General Checks to Avoid Headaches
  • File Permissions: Make sure your web server user (Apache = apache, Nginx = nginx) has read access to your framework files. Run:
    # For Apache
    sudo chown -R apache:apache /var/www/your-framework
    # For Nginx
    sudo chown -R nginx:nginx /var/www/your-framework
    
    Set directory permissions to 755 and file permissions to 644 for security.
  • Security Groups: Ensure your EC2 instance's security group allows inbound traffic on ports 80 (HTTP) and 443 (HTTPS, if you're using SSL).
  • Test It Out: Visit your site with a URL like http://your-ec2-ip/controller/method—it should load without needing index.php in the path.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:41:04