This is driving me nuts and I dont understand how this works automatically. I have nginx as my web server and I have installed Joomla that has URL Rewriting which removes the index.php
from the URL. Before, when I was using Apache, I have to enable the .htaccess with RewriteEngine On
in order for this to work. But with Nginx, when I enable "Use URL Rewriting" it works automatically. I only use Nginx that passes the php files to php-fpm. Thats all. I havent added any special rewrite rule other than what was given in Joomla docs. I dont understand how 'Use URL Rewriting' just works automatically when I enable it since there is no .htaccess for Nginx.
The Joomla docs on this topic didint help neither. In second step it says
Enable the Use Apache mod_rewrite/URL rewriting option and Save: This option uses the Apache mod_rewrite function to eliminate the "index.php" portion of the URL.
.....
If this option causes errors, please see How to check if mod rewrite is enabled on your server. If it is not enabled and you have access to the file apache/conf/httpd.conf, open that file and check if the line LoadModule rewrite_module modules/mod_rewrite.so is uncommented. If necessary, uncomment the line and restart the Apache web server.
No idea why this is added in an Nginx configuration since there is no mod_rewrite in Nginx. The URL Rewriting in Joomla backend says this:
Use URL rewriting Select to use a server's rewrite engine to catch URLs that meet specific conditions and rewrite them as directed. Available for IIS 7 and Apache. Apache users only! Rename htaccess.txt to .htaccess before activating. Rename web.config.txt to web.config and install IIS URL Rewrite Module before activating.
It says nothing about Nginx but still it works. I am scratching my head here. Can someone tell me how the Joomla's index.php is removed so easily in Nginx? This is my Nginx Vhost configuration:
server {
listen 80;
server_name example.com;
root /var/www/example/public_html;
index index.php index.html index.htm default.html default.htm;
access_log /var/log/nginx/accn_access.log;
error_log /var/log/nginx/accn_error.log;
##
# JOOMLA SEF
##
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
##
# PHP scripts to FastCGI
##
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
See.. its a pretty straight forward configuration. Where is the magic happening?
The magic happens here:
This means that nginx checks first if the requested file or directory exists on the filesystem. If a file does not exist, it passes the request to Joomla, and passing the original URI to the
q
parameter.