Reputation: 307
I have a Codeigniter PHP app, and I have configured my nginx conf to pretty up some of the URLs. For example, /index.php/home will rewrite to /home. Now I am trying to configure nginx to rewrite http://mysite.com/sitemap.xml to /sitemap (which constructs the xml file). This is what I am using in my nginx conf:
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /index.php/sitemap last;
}
For some reason, the above doesn't work, I think another rule might be conflicting, but I can't figure out a workaround. Can anyone help? (full config file is below)
server {
listen 80;
root /www/myapp;
index index.php index.html index.htm;
access_log /var/log/nginx/access.log;
if (-f /www/myapp/application/errors/error_503.html)
{
return 503;
}
# canonicalize codeigniter url end points
#/home will redirect to /
if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
############THIS PART IS NOT WORKING
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /index.php/sitemap last;
}
####################################
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
## Default location
location / {
root /www/myapp;
index index.html index.htm index.php;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
root /www/myapp;
}
## Parse all .php file in the www directory
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/myapp/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
## catch all
#error_page 404 /index.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
Upvotes: 0
Views: 14488
Reputation: 341
I use it in on my website:
# Sitemap.xml
location /sitemap.xml {
alias /venvs/mobilepro/src/mobilepro/static/maps/map.xml;
}
Upvotes: 1
Reputation: 49
Problem above is that REQUEST_URI remains as /sitemap.xml instead of /sitemap, so CI can't route it properly. Try changing "last" to "redirect":
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /sitemap redirect;
}
Also consider using LOCATION instead of IF (avoid IF in general):
location = /sitemap.xml
{
rewrite .* /sitemap redirect;
}
Hope that helps
Upvotes: 3