Nginx Redirect Loop, Remove Index.PHP from Url

nginx redirect loop, remove index.php from url

Great question, with the solution similar to another one I've answered on ServerFault recently, although it's much simpler here, and you know exactly what you need.

What you want here is to only perform the redirect when the user explicitly requests /index.php, but never redirect any of the internal requests that end up being served by the actual index.php script, as defined through the index directive.

This should do just that, avoiding the loops:

server {
index index.php;

if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}

location / {

# ...
}
}

nginx php friendly URL redirection without interfering with index.php causing /index

You're likely experiencing the issue with your browser having your prior 301 Moved Permanently redirects in its cache, thus some of your newer code would not be having any effect.

Clear the cache, and try something like this:

index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2; }

The above line supports stripping out not only index.php, but also just index as you might have from your earlier question, as well as just .php, yet it preserves the possible query string from $args, too.

To avoid having support for stripping just index, still supporting the rest of the features as above, use the following instead:

if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index)?\.php(\?.*)?$) {   return 301 /$1$2;   }

P.S. The original explanation of this trick is here: nginx redirect loop, remove index.php from url.

Removing index files from the URL with Nginx

Try this ...

server  {
listen 80;
server_name examplesite.com;
# redirect non-www to www. for canonical urls
# proper way to do this
rewrite ^ http://www.examplesite.com$request_uri? permanent;
}

server {
listen 80;
server_name www.examplesite.com;

error_log /srv/http/nginx/examplesite.com/log/nginx-error.log;
access_log /srv/http/nginx/examplesite.com/log/nginx-access.log;

root /srv/http/nginx/examplesite.com/root;

# add index under server as you have done for root
index index.php index.html;

location / {
# drop the "=404". Nginx will return 404 by itself if not found.
# note the "last" should not be added to "try_files"
# whether you need something after "$uri/" depends on your setup.
# this should do for most unless running WP etc
try_files $uri $uri/;
}

location ~ \.php$ {
# Return '400 Bad Request' for malformed URLs
# See: http://wiki.nginx.org/Pitfalls#Pass_Non-PHP_Requests_to_PHP.
location ~ \..*/.*\.php$ {
return 400;
}

# Rewrite "index.php" requests
rewrite ^(.*)index.php(.*)$ $1$2 permanent;

# continue for other php and rewritten "index.php" requests
include fastcgi.conf;
fastcgi_param PHP_ADMIN_VALUE "error_log=/srv/http/nginx/examplesite.com/log/php-error.log";
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}

...

}

Not 100% about the index.php rewrite line as there is a chance it might put you in a redirect loop. If that is the case, then the answer is to just serve the index.php requests but change all links in the application to remove them so that they disappear over time.

nginx : rewrite rule to remove /index.html from the $request_uri

I use the following rewrite in the top level server clause:

rewrite ^(.*)/index\.html$ $1 permanent;

Using this alone works for most URLs, like http://example.com/bar/index.html, but it breaks http://example.com/index.html. To resolve this, I have the following additional rule:

location = /index.html {
rewrite ^ / permanent;
try_files /index.html =404;
}

The =404 part returns a 404 error when the file is not found.

I have no idea why the first rewrite alone isn't sufficient.



Related Topics



Leave a reply



Submit