Convet htaccess to nginx - .htaccess

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;
}

Related

How to convert apache .htaccess to nginx

How to convert below condition for nginx,
RewriteEngine on
RewriteRule ^([^\.]+)/?$ index.php?$1 [L]
RewriteRule ^([^\.]+)/([^\.]+)/$ index.php?$1 [L]
RewriteRule ^([^\.]+)/([^\.]+)$ index.php?$1 [L]
RewriteRule ^([^\.]+)/([^\.]+)/([^\.]+)$ index.php?$1 [L]
RewriteRule ^([^\.]+)/([^\.]+)/([^\.]+)/$ index.php?$1 [L]
RewriteRule ^([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)$ index.php?$1 [L]
RewriteRule ^([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)/$ index.php?$1 [L]
I need to convert the apache .htaccess to Nginx configuration, if anyone knows the solution please help me, thanks.
Already I have converted .htaccess file to Nginx configuration by using some online tools, but it's not working.
If I put the URL in the browser automatically downloaded the index.php file. I have checked info.php but fpm also working fine. I don't know how to fix this issue.
here i have mention my Nginx conf file:
server {
server_name example.com;
root /home/example/public_html;
index index.php;
access_log /var/log/nginx/example/example-access.log;
error_log /var/log/nginx/example/example-error.log;
location / {
rewrite ^/([^\.]+)/?$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)/$ /index.php?$1 break;
}
location ~ ^/(?:\.htaccess) {
deny all;
}
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.6-fpm-example.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
The GetPageSpeed converter provides the correct result:
server {
server_name example.com;
rewrite ^/([^\.]+)/?$ /index.php?$1 last;
rewrite ^/([^\.]+)/([^\.]+)/$ /index.php?$1 last;
rewrite ^/([^\.]+)/([^\.]+)$ /index.php?$1 last;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)$ /index.php?$1 last;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/$ /index.php?$1 last;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)$ /index.php?$1 last;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)/$ /index.php?$1 last;
}
Note the last directive. This is correct. It will make sure NGINX will start location search again to see which directives are applicable to rewritten URI /index.php. Then it finally ends up in your location ~ [^/]\.php(/|$) { ... } and directives for processing with PHP-FPM will apply.
With break, things will not work, because there's no new location search after rewriting URL. The URI stays /index.php but directives from location ~ [^/]\.php(/|$) { ... } will not be applied because NGINX didn't "go to" apply directives from it.
UPDATE 2022-03-18
As pointed out by #Danila Vershinin the rewrite statements should end with last instead of break.
You can try something like this
location / {
rewrite ^/([^\.]+)/?$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)$ /index.php?$1 break;
rewrite ^/([^\.]+)/([^\.]+)/([^\.]+)/([^\.]+)/$ /index.php?$1 break;
}
or use a converter or another converter.

migrate htaccess rewrite to nginx

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;
}
}

Nginx redirect non-www to www of domain with custom port

I have a domain with a custom port that would want to redirect from non-www to www. For example: from example.com:6789 to www.example.com:6789.
My current .conf
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server{
listen 6789;
server_name example.com;
return 301 http://www.example.com:6789$request_uri;
}
What should i do to redirect?
Use rewrite instead of return
Following config should work
server{
listen 6789;
server_name example.com;
rewrite ^/$ http://www.example.com:6789 permanent;
}

nginx location rule interference

I have this Nginx rule:
location /fr {
rewrite ^/(fr)(?:/([^/]+))?$ /$2?lang=$1 last;
}
I have pages such as:
example.com/en/some-page
example.com/fr/some-page
example.com/fr
...
But I also have a page which starts by fr:
example.com/free-plan // doesn't work
example.com/fr/free-plan // doesn't work either
How can I modify my Nginx rule so that the /fr rule doesn't interference with my free-plan page?
Ps: I use PHP and rules to service page without the extension:
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
I would suggest that you treat /fr as a special case and use location /fr/ to avoid side-effects from pages that begin with fr.
For example:
location = /fr {
rewrite ^/(.*)$ /?lang=$1 last;
}
location /fr/ {
...
}
See this document for details.

Nginx Rewrite Multiple Query Parameters

I am having problems creating a rewrite rule that match my .htaccess on Nginx, I am hoping someone can point me to the right direction.
My .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|app|assets|upload|api)
RewriteRule ^([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) index.php?class=$1&action=$2&param=$3 [L]
My nginx configuration
location / {
rewrite ^/([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) /index.php?class=$1&action=$2&param=$3 break;
}
Query params are optional for example:
site.com/
site.com/customers
site.com/customers/add
site.com/customers/edit/1
I cannot rewrite the app code since iam only migrating from apache to nginx.
Any help will be appreciated.
Thanks.
You could structure the solution like this:
root /path/to/root;
index index.php;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) /index.php?class=$1&action=$2&param=$3 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
# fastcgi stuff
}
I have not checked your regular expression. The try_files directive is documented here.

Resources