How to convert .htaccess configuration to nginx?
My .htacess:
Options -MultiViews
RewriteEngine On
RewriteBase /mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I tried this:
nginx configuration
location /mvc/public/ {
if (!-e $request_filename){
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 [QSA,L];
}
}
But this did not work!
Could someone help me?
The [QSA,L] is not nginx syntax - see this document for details.
location /mvc/public/ {
if (!-e $request_filename) {
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
}
}
Similar behaviour can be accomplished with try_files rather than the if:
location /mvc/public/ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
}
See this document for details.
Related
I'm currently migrating a website from an Apache build that contains the following .htaccess rules:
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|admin|captcha|print|pdf\.php|robots\.txt)
RewriteRule (.*) index.php?req=$1 [QSA,L]
</ifmodule>
I've setup NGINX and added the following location block:
location / {
try_files $uri /index.php?req=$uri$is_args$args;
}
This works for most pages, but some internal pages that handle query string are failing. After some debugging, I found that dumping $_GET was returning differently. The Apache server is returning:
array(2) { ["req"]=> string(8) "checkout" ["quick"]=> string(23) "email#address.com" }
which works perfectly for this old website, but the NGINX server is returning:
array(1) { ["req"]=> string(39) "/checkout?quick=email#address.com" }
Can anybody help me understand what's going on? It seems there's an issue with me passing req, as well as additional query params.
Update: I've managed to partly resolve this with the following NGINX settings, but it's annoying I have to manually exclude every path I don't want to be rewritten:
location / {
try_files $uri $uri/ /index.php?$is_args$args;
rewrite ^/(.*)$ /index.php?req=$1 last;
}
location /admin {
try_files $uri $uri/ /admin/index.php?$is_args$args;
}
location /js/ {
# Do nothing. nginx will serve files as usual.
}
location /css/ {
# Do nothing. nginx will serve files as usual.
}
location /images/ {
# Do nothing. nginx will serve files as usual.
}
I managed to resolve my issue with the following:
location / {
try_files $uri $uri/ /index.php?$is_args$args;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?req=$1 last;
}
}
which should prevent rewriting of non-request filenames.
This the htacess code
RewriteEngine on
ErrorDocument 404 /404.php
RewriteRule ^watch/(.*)/([0-9]+)/(.*)/?$ search.php?search=$1&page=$2&token=$3
RewriteRule ^video/(.*)/(.*)/?$ watch.php?link=$1&name=$2
RewriteRule ^privacy/?$ privacy.php
RewriteRule ^dmca/?$ dmca.php
RewriteRule ^contact/?$ contact.php
RewriteRule ^send/?$ send.php
RewriteRule ^surprise/?$ surprise.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
what should be the correct nginx config?
nginx configuration
error_page 404 /404.php;
location /watch {
rewrite ^/watch/(.*)/([0-9]+)/(.*)/?$ /search.php?search=$1&page=$2&token=$3;
}
location /video {
rewrite ^/video/(.*)/(.*)/?$ /watch.php?link=$1&name=$2;
}
location /privacy {
rewrite ^/privacy/?$ /privacy.php;
}
location /dmca {
rewrite ^/dmca/?$ /dmca.php;
}
location /contact {
rewrite ^/contact/?$ /contact.php;
}
location /send {
rewrite ^/send/?$ /send.php;
}
location /surprise {
rewrite ^/surprise/?$ /surprise.php;
}
location / {
}
I'm having a problem converting an appache .htaccess file work with nginx.
The .htaccess file
<ifModule mod_rewrite.c>
Allow from 127.0.0.1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index(|/)$ index.php
RewriteRule ^logout(|/)$ logout.php
RewriteRule ^keeping/([^/.]+)(|/)$ keeping.php?s=$1
</ifModule>
My Conversion:
location / {
if (!-e $request_filename){
rewrite ^/index(|/)$ /index.php;
}
}
location /logout {
rewrite ^/logout(|/)$ /logout.php;
}
location /kyhsadminpanel {
rewrite ^/keeping/([^/.]+)(|/)$ /keeping.php?s=$1;
}
doesn't work.
It always shows "404 not found" for url's such as /kyhsadminpanel/keeping/index
and /kyhsadminpanel/keeping/news...
You might want to note you used: https://winginx.com/en/htaccess
I tend to use: https://timmehosting.de/htaccess-converter
Here is the result from them.
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/index(|/)$ /index.php ;
}
rewrite ^/logout(|/)$ /logout.php ;
rewrite ^/keeping/([^/.]+)(|/)$ /keeping.php?s=$1 ;
How do I convert these rules from .htaccess (apache) to Nginx conf?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1-$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I use these rules to rewrite requests like:
/someurl1/ => someurl.html (/company/ to /company.html
/someurl1/someurl2/ => someurl-someurl2.html (/projects/3/ to /projects-3.html)
I've already tried this (doesn't work):
if (!-e $request_filename) {
rewrite ^/([^/]+)/$ /$1.html;
rewrite ^/([^/]+)/([^/]+)/$ /$1-$2.html;
}
try_files $uri $uri/ =404;
Where am I wrong?
I have tested the following and it appears to work:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/([^/]+)/$ /$1.html last;
rewrite ^/([^/]+)/([^/]+)/$ /$1-$2.html last;
return 404;
}
The RewriteCond %{REQUEST_FILENAME} !-f test is actually accomplished by the try_files directive.
See this for details.
I am trying to convert the code from the htaccess to Nginx but without any success, and yes i have tried all the online converter but it did not help, so can any here on stockoverflow help me please? I'm going crazy soon :-p (Here is the code from the htaccess)
RewriteCond %{HTTP_HOST} ^www.WEBSITE.com
RewriteRule (.*) http://WEBSITE.com/$1 [R=301,L]
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Please try the following:
server {
listen 80;
server_name www.website.com;
return 301 $scheme://website.com;
}
server {
listen 80;
server_name website.com;
root /path/to/root;
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/([a-z]+)/?([0-9a-zA-Z]*)/?.*$ /index.php?a=$1&q=$2 last;
}
}