migrate htaccess rewrite to nginx - .htaccess

I have the followinf rewrite rule on htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^?]+?)(?:\.php)?\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
The rule redirects "lang" parameter to subdirectory:
http://www.www.www/somepage?lang=en -> http://www.www.www/en/somepage
How can i create the same rule on NGINX ?

Could you please place following Rules into your nginx Rule file. I have placed a file named so_question in path /etc/nginx/sites-enabled and ran the service on 8081 in my localhost and these worked perfectly fine(tested by curl command). By the way I am no expert in nginx, I spent ample amount in learning to solve this :) I hope this helps.
As per your shown htaccess this assumes you want to redirect a url from http://localhost:8181/test.php?lang=en_abc123_singhblabla TO http://localhost:8181/en_abc123_singhblabla-test(mentioned this in comments also inside Rule file).
cat so_question
server {
listen 8181;
listen [::]:8181 ipv6only=on default_server;
server_name localhost;
##Actual redirect rule which redirects from a URL eg-->
##http://localhost:8181/test.php?lang=en_abc123_singhblabla -->
##http://localhost:8181/en_abc123_singhblabla-test
if ($request_uri ~ "^/([^.]*)\.php\?lang=(.*)") {
set $original_path $1;
set $args1 $2;
set $args "";
rewrite ^ "$scheme://$host/${args1}-${original_path}" permanent;
}
}

Related

Converting htaccess rewrite rule to nginx

I have trouble figuring out how to rewrite this htaccess rule to a nginx rule. It's for a legacy project that uses a static asset version in the url.
The file on the server is for example located in:
assets/js/jquery.min.js
The url to fetch it is:
assets/js/jquery.min.1661243858.js
The htaccess rule is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?\/)*?([a-zA-Z\.\-]+)(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1$2$4 [L]
</IfModule>
I tried some converters and found a blog post going over this. But it doesn't seem to be working. I'm using ddev as my environment. Current nginx rule that I set in .ddev/nginx_full/nginx-site.conf, I removed #ddev-generated:
location ~* (.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif|webp)$ {
try_files $uri $1.$2;
}
So the problem was that I assumed doing ddev start applied the changes made in .ddev/nginx_full/nginx-site.conf. It doesn't. I needed to use ddev restart. The nginx rule is correct.

Convet htaccess to nginx

I have transferred my server apache to Nginx but I face problem on our .htaccess rewrite rule
I have this rule on .htaccess
RewriteEngine On
RewriteRule ^dl/(\w+)/(\w+)/(.*)/?$ download.php?pt=$1&tk=$2&flen=$3 [L,QSA,NC]
I try this code in Nginx but not working
http{
server {
listen 80;
server_name *.oursitename.com;
location / {
rewrite ^dl/(\w+)/(\w+)/(.*)/?$ download.php?pt=$1&tk=$2&flen=$3 last;
}
}
}
I put location like location / because our download.php file in public_html main area.
please tell me how to it is working fine help me thanks
With the following rewriter:
server {
server_name example.com;
rewrite ^/dl/(\w+)/(\w+)/(.*)/?$ /download.php?pt=$1&tk=$2&flen=$3 last;
}

converting apache rewrite to nginx url rewrite

can anybody help me to convert .htacess file to nginx rewrite rule
my htaccess blew
RewriteEngine On
RewriteRule ^video/([^/]*)/([^/]*)\/$ /index.php?page=details&title=$1&id=$2 [L]
RewriteRule ^video/([^/]*)\.html$ /index.php?page=result&q=$1 [L]
i try apply on here:
/etc/nginx/sites-available/default
# nginx configuration
location /video {
rewrite ^/video/([^/]*)/([^/]*)\/$ /index.php?page=details&title=$1&id=$2 break;
rewrite ^/video/([^/]*)\.html$ /index.php?page=result&q=$1 break;
}
not working..

Nginx - Rewrite timestamped assets

I'm currently in the process of moving from Apache to Nginx and am having problems with one of the rewrite rules. I have the following rule in my old .htaccess:
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
How would I rewrite this rule for Nginx? Everything I try either doesn't work or simply prevents Nginx from reloading.
You can use this rule:
Location ~ \.(css|js)$ {
rewrite "^(.+)\.\d{10}\.(css|js)$" $1.$2;
}

Translate htaccess rewrite rule for nginx

I would like to translate the following apache htaccess rewrite rule for nginx.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.+) /index.php
Any ideas how to do it?
The apache rewrite rules are explained clearly in the other question, especially that special dash "-" rule for non-substitution. This can be rewritten as a try_files directive in nginx.
Your question doesn't list a rule for the empty string (home page) (only has non-empty string (.+)). So I'll assume you've had one rule for the home page somewhere else. The rule in nginx could be
location = / {
# your nginx rule matching the empty string (for your home page)
}
# all other locations try in the order: file => directory => index.php
location / {
try_files $uri $uri/ /index.php;
}

Resources