Rewrite url in nginx config - .htaccess

I want to rewrite url
https://www.beecow.info/product/whosaler-san-pham-shop-dang-590-34-390-k-p345954
to
https://www.beecow.info/whosaler-san-pham-shop-dang-590-34-390-k-p345954
I want to remove apart /product/ And here is my nginx config
server {
listen 80;
server_name beecow.info;
return 301 https://www.beecow.info$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.beecow.info localhost;
root /var/www/html/;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
if ($http_x_forwarded_proto != "https") {
rewrite ^(.*)$ https://$server_name$1 permanent;
}
location ~* ^/product/(.*){
rewrite ^/product/(.*)$ /$1 last;
}
location / {
try_files $uri$args $uri$args/ /index.html;
}
But It can't work.The url not change anymore.

Your rewrite doesn't change it:
location ~* ^/product/(.*){
rewrite ^/product/(.*)$ /$1 last;
}
Something like this will do the job (untested):
location ~ /product/(.*)$ {
rewrite ^ /$1?$args permanent;
}

Related

Nginx access to images in backend folder

My app store images on the backedn side in nodejs folder: /images.
After publish it to Ec2 I don't have an access to these images by url like: www.domain.com/image01.jpg
Configuration in /etc/nginx/sites-available/default:
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root /opt/front-end;
try_files $uri /index.html;
}
location /images/ {
root /opt/back-end/;
}
location /api/ {
proxy_pass http://localhost:4000/;
}
}
Is it the configuration problem or permission or something else? I will be grateful for help!
The correct configuration should be:
server {
charset utf-8;
listen 80 default_server;
server_name _;
location / {
root /opt/front-end/;
try_files $uri /index.html;
}
location /api {
proxy_pass http://localhost:4000;
}
location /images {
autoindex on;
alias /opt/back-end/images/;
}
}

Change format from htaccess to nginx

I have apache server with htaccess file like this:
RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^test/\$([^/]+)/([^/]+\.php)$ test/$2?VIRTUAL_DIRECTORY_NAME=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^.*$ [NC]
RewriteRule ^test/([^/]+)/(.*)$ $1/$2 [L]
And I am trying to convert it for nginx server.
I cant figure out how to transfor virtual directory to correct format.
This is my nginx config file:
server {
listen 80;
listen 443 ssl;
server_name admin.dev;
root "/home/vagrant/admin";
index index.html index.htm index.php;
charset utf-8;
rewrite_log on;
location /api/ {
rewrite ^/api/(.*)$ /api.php?$1 last;
}
location /res_partners/ {
rewrite ^res_partners/\$([^/]+)/([^/]+\.php)$ res_partners/$2?VIRTUAL_DIRECTORY_NAME=$1 last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/admin.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/admin.dev.crt;
ssl_certificate_key /etc/nginx/ssl/admin.dev.key;
}
Any help?
Thanks
The first rule seems to rewrite /test/$something/somescript.php to /test/somescript.php?VIRTUAL_DIRECTORY_NAME=$something and these are literal $ characters. Your location ~ \.php$ { ... } container already matches any URI that ends with .php. So the first rule can be placed at the top of that container:
location ~ \.php$ {
rewrite ^/test/\$([^/]+)/([^/]+\.php)$ /test/$2?VIRTUAL_DIRECTORY_NAME=$1&$args break;
...
}
The second rule seems to rewrite /test/somedir/something to /somedir/something (where something is not a php script).
This can be achieved with a prefix location:
location /test/ {
rewrite ^/test/([^/]+)/(.*)$ /$1/$2;
}

Nginx Rewrite Not working

I have move my site to nginx but can get .htaccess roules right
below ius my code
server {
listen 80;
server_name mysite.com;
root /usr/share/nginx/mysite;
index index.php index.html index.htm;
location / {
rewrite ^/(.*)$ /members.php?id=$1 last;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
i want to display user url to be
mysite.com/jonny
instead of
mysite.com/memner.php?id=jonny
when i use
rewrite ^/(.*)$ /members.php?id=$1 last;
site don't load please help.
got fix for this
if (!-e $request_filename) {
rewrite /(.*) /members.php?id=$1 last ;
}

prestashop/nginx multi lang "no input file specified" on index.php request

we have prestashop running smoothly on nginx;
when we enabled multi language on the site, the below issue is faced:
usually after payment, without multi lang, you are redirected back to this page:
http://www.domain.com/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c04910968
on our apache environemnt, this works well
on nginx,
what happened when multi language was enabled is when you reach the above link, the lang parameter is added to the url (when it should not):
http://www.domain.com/en/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c049109689ecf36499b
or
http://www.domain.com/fr/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c049109689ecf36499b
which causes the user to fall on a prestashop error "input file not specified"
the above redirection should not happen, once an order is done, the following URL must work without redirection nor adding the 'en' or 'fr' code before index.php:
http://www.domain.com/index.php?controller=order-confirmation&id_cart=21293&id_module=83&key=e1dbc21a0e7a8c049109689ecf36499b
the nginx configuration is as follows:
location /favicon {
alias /var/www/vhosts/11footballclub.com/httpdocs/img/facicon.ico;
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/vhosts/11footballclub.com/httpdocs$fastcgi_script_name;
fastcgi_param PHP_VALUE max_execution_time=180;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite "^/c/([0-9]+)(\-[_a-zA-Z0-9-]*)/(.*)\.jpg$" /img/c/$1$2.jpg last;
rewrite "^/c/([_a-zA-Z-]+)/(.*)\.jpg$" /img/c/$1.jpg last;
rewrite "^/([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/(\P{M}\p{M}*)*\.jpg$" /img/p/$1-$2$3.jpg last;
rewrite "^/([0-9]+)\-([0-9]+)/(\P{M}\p{M}*)*\.jpg$" /img/p/$1-$2.jpg last;
rewrite "^/([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$1$2.jpg last;
rewrite "^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$1$2$3.jpg last;
rewrite "^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite "^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?/(\P{M}\p{M}*)*\.jpg$" /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
#rewrite "^/index.php\?controller=http(.*)$" $1 last;
rewrite "^/([0-9]+)\-(\P{M}\p{M}*)+\.html(.*)$" /index.php?controller=product&id_product=$1$3 last;
rewrite "^/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=category&id_category=$1$3 last;
rewrite "^/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$" /index.php?controller=product&id_product=$2$4 last;
rewrite "^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$" /index.php?controller=supplier&id_supplier=$1$3 last;
rewrite "^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$" /index.php?controller=manufacturer&id_manufacturer=$1$3 last;
rewrite "^/content/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=cms&id_cms=$1$3 last;
rewrite "^/content/category/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$" /index.php?controller=cms&id_cms_category=$1$3 last;
rewrite "^/module/([_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)$" /index.php?fc=module&module=$1&controller=$2 last;
rewrite ^/page-not-found$ /index.php?controller=404 last;
rewrite ^/address$ /index.php?controller=address last;
rewrite ^/addresses$ /index.php?controller=addresses last;
rewrite ^/authentication$ /index.php?controller=authentication last;
rewrite ^/best-sales$ /index.php?controller=best-sales last;
rewrite ^/cart$ /index.php?controller=cart last;
rewrite ^/contact-us$ /index.php?controller=contact-form last;
rewrite ^/discount$ /index.php?controller=discount last;
rewrite ^/guest-tracking$ /index.php?controller=guest-tracking last;
rewrite ^/order-history$ /index.php?controller=history last;
rewrite ^/identity$ /index.php?controller=identity last;
rewrite ^/manufacturers$ /index.php?controller=manufacturer last;
rewrite ^/my-account$ /index.php?controller=my-account last;
rewrite ^/new-products$ /index.php?controller=new-products last;
rewrite ^/order$ /index.php?controller=order last;
rewrite ^/order-follow$ /index.php?controller=order-follow last;
rewrite ^/quick-order$ /index.php?controller=order-opc last;
rewrite ^/order-slip$ /index.php?controller=order-slip last;
rewrite ^/password-recovery$ /index.php?controller=password last;
rewrite ^/prices-drop$ /index.php?controller=prices-drop last;
rewrite ^/search$ /index.php?controller=search last;
rewrite ^/sitemap$ /index.php?controller=sitemap last;
rewrite ^/stores$ /index.php?controller=stores last;
rewrite ^/supplier$ /index.php?controller=supplier last;
location ~* \.(gif)$ {
expires 2592000s;
}
location ~* \.(jpeg|jpg)$ {
expires 2592000s;
}
location ~* \.(png)$ {
expires 2592000s;
}
location ~* \.(css)$ {
expires 604800s;
}
location ~* \.(js|jsonp)$ {
expires 604800s;
}
Add this location block to your config file.
location ~ (/lang1|/lang2)/index\.php$ {
try_files $uri $uri/ /index.php?$args;
}
Where lang1 and lang2 are languages used in your website (for example 'en', 'ru' and etc.). You can add more languages by appending |/language_name inside brackets.
P.s. don't forget to restart/reload your nginx server after modifications.
Here's an example of configuration that works pretty well with Nginx, PrestaShop v1.6.1.x (two languages enabled: EN/FR), PHP-FPM 7.2 and an SSL certificate from Let's Encrypt.
If you copy/paste this example (in your /etc/nginx/sites-enabled folder), please remember to:
Replace 'myshop.com' by your domain name
Replace 'myadminfolder' by your PrestaShop admin folder name
Replace '(/fr|/en)' with the language codes you are actually using
Generate your SSL certificate using Let's Encrypt certbot tool (optional, remove this section if you don't want to use SSL)
Make sure your PHP-FPM socket file is located in /var/run/php/php7.2-fpm.sock, otherwise modify this path
Thanks #Canarius for the tip!
server {
listen [::]:80;
listen 80;
server_name myshopname.com www.myshop.com
root /var/www/myshopname.com/;
index index.php index.html;
access_log /var/log/nginx/myshopname.com.access_log;
error_log /var/log/nginx/myshopname.com.error_log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
auth_basic off;
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 1;
gzip_buffers 16 8k;
gzip_http_version 1.0;
gzip_types application/json text/css application/javascript;
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
location ~ (/fr|/en)/index\.php$ {
try_files $uri $uri/ /index.php?$args;
}
try_files $uri $uri/ /index.php?$args;
location /myadminfolder/ {
if (!-e $request_filename) {
rewrite ^/.*$ /myadminfolder/index.php last;
}
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_keep_conn on;
include /etc/nginx/fastcgi_params;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.myshopname.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.myshopname.com/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
}

301 redirect is not removing the original paths in NGINX

I was trying to redirect a particular URL to a new URL using rewrite | 301 redirect catered by NGINX.
if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
rewrite ^.*$ playlist/all/ redirect;
}
If my request URI is http://servername.com/playlist/show/531a5aaefd3401705c000a32, then it should redirect to http://servername.com/playlist/all/
But what's happening right now is that it the original paths from old URL are still there:
http://servername.com/playlist/show/playlist/all/
The /playlist/show/ slashes/paths are still there, so it's redirecting to the wrong URI.
I'm pretty new in handling NGINX. Please bear with me. Thanks!
EDITS:
Here's my conf to be clearer:
server {
listen 80;
server_name site.servername.com;
server_tokens off;
charset utf-8;
root /home/site/stm/public;
location / {
index index.php;
# auth_basic "Restricted";
# auth_basic_user_file /home/site/stm/.htpasswd;
}
location = /favicon.ico {
access_log off;
log_not_found off;
empty_gif;
expires 30m;
}
if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
rewrite ^.*$ playlist/all/ redirect;
}
if ($request_uri = "/favicon.ico") {
rewrite ^.*$ /favicon.ico last;
}
if ($request_uri !~* ^/(?:static|favicon.ico)) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
You're doing it wrong: http://wiki.nginx.org/Pitfalls
You should never use if ($uri or if ($request_uri because it's just a very bad method of making location.
location =/playlist/show/531a5aaefd3401705c000a32 {
return 302 /playlist/all/;
}
Simple, fast, no regexps.
Reference to study:
http://nginx.org/r/location
Converting rewrite rules
How nginx processes a request

Resources