如何在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.
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 NonetoAllowOverride 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
DocumentRootpoints directly to Laravel'spublicfolder, 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
- File Permissions: Make sure your web server user (Apache =
apache, Nginx =nginx) has read access to your framework files. Run:
Set directory permissions to# For Apache sudo chown -R apache:apache /var/www/your-framework # For Nginx sudo chown -R nginx:nginx /var/www/your-framework755and file permissions to644for 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 needingindex.phpin the path.
内容的提问来源于stack exchange,提问作者Rajat Masih

