I have a URL:
https://www.example.com/catalog
I want to access it via:
https://www.example.com/products
If I redirect this url, then when I try to follow the url:
https://www.example.com/products/bicycle
https://www.example.com/products/car
https://www.example.com/products/tshirt
etc..
That brings me back:
https://www.example.com/products
How can I redirect ONLY www.example.com/catalog to ONLY the www.example.com/products url?
I tried to do:
location /products/ {
return 301 /catalog/;
}
and:
rewrite /products/ /catalog/ permanent;
and:
if ($is_args = '') {
rewrite ^/catalog\$ /products permanent;
}
It redirects me from /products/ to /catalog/ as I wanted. But if I go to other url /products/car /products/tshirt/ /products/bicycle and the like, then I am also transferred to /catalog, but I don’t need it. Help me please.
My config file:
server {
server_name sub.url.ru;
root /home/suburl/public_html/;
index index.php;
access_log /home/sportmen/sportmen.ru/logs/access.log;
error_log /home/sportmen/sportmen.ru/logs/error.log;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on; }
if ($request_uri ~ ^(.*)/index.(html|php)) { return 301 $1/$is_args$args; }
location / { try_files $uri $uri/ /bitrix/urlrewrite.php$is_args$args; }
location ~* @.*\.html$ { internal; }
include /etc/nginx/vhosts-includes/suburl/redirect.conf;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/suburl/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/suburl/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
###phpMyadmin###
location /phpmy123 {
alias /usr/share/phpmyadmin;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_ignore_client_abort off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
access_log off;
log_not_found off;
expires 1M;
}
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
###end-phpMyAdmin###
server {
if ($host = sub.url.ru) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name sub.url.ru;
return 404; # managed by Certbot
CodePudding user response:
location /products/ {
return 301 /catalog/;
}
Here /product/
is interpreted as a prefix. For nginx to interpret it as exact match, you need to use a modifier, e.g. =
:
location = /products/ {
return 301 /catalog/;
}
Here /product/bicycle
is no longer a match for this location.
See other location modifiers here : https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#locations