Answer the question
In order to leave comments, you need to log in
How to make 301-redirect in Nginx from index.php to site root(/)?
Good afternoon! I'm moving my website from shared hosting to VDS. The whole problem is that Apache + php-cgi worked on the hosting, and I set up the VDS on Nginx + php-fpm. Accordingly, all rules in .htaccess are ignored.
At the moment, most of the problems with mod_rewrite have been visually solved, but one significant problem remains: it is necessary to implement a 301-Redirect from site.ru/index.php and site.ru/index.html to site.ru/.
With index.html it was possible to solve by introducing the directive:
location /index.html {
rewrite ^(.*)$ http://site.ru/ permanent;
}
if ($request_uri ~* "^(.*/)index\.php") {
return 301 $1;
}
##Redirect from index.php to root
RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^index\.php$ http%2://site.ru/ [R=301,L]
##END Redirect from index.php to root
##Redirect from index.html to root
RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^index\.php$ http%2://site.ru/ [R=301,L]
##END Redirect from index.html to root
Answer the question
In order to leave comments, you need to log in
delete locations with index files and write
index index.php index.html;
server {
listen *:80 default_server;
server_name localhost;
root /var/www/web/sites/$host;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
## try_files $uri =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_ignore_client_abort off;
}
}
include phpmyadmin.conf;
location /catalog.html {
rewrite ^(.*)$ http://site.ru/ permanent;
}
location /index.html {
rewrite ^(.*)$ http://site.ru/ permanent;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question