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.
Related
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 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.
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;
}
}
I got the following .htaccess code for a Magento plugin, could someone help me convert it to valid Nginx rewrites? I'm having a really tough time getting this down. It's for a plugin that rewrites and caches Magento URL's.
The original editor of the module couldn't help me. I'm sure there are lots of people using Nginx and wanting to use this plugins functionality!
# static rewrite - home page
RewriteCond %{HTTP_COOKIE} store=default
RewriteCond %{HTTP_COOKIE} !artio_mturbo=.*
RewriteCond %{REQUEST_URI} ^/magento/$
RewriteCond %{QUERY_STRING} !.+
RewriteCond /var/ww/var/turbocache/default.html -f
RewriteRule .* var/turbocache/default.html [L]
# static rewrite - other pages
RewriteCond %{HTTP_COOKIE} store=default
RewriteCond %{HTTP_COOKIE} !artio_mturbo=.*
RewriteCond %{REQUEST_URI} /magento/(.*)\.html$ [NC]
RewriteCond %{QUERY_STRING} !.+
RewriteCond /var/www/var/turbocache/magento/default/%1.html -f
RewriteRule .* var/turbocache/magento/default/%1.html [L]
# store view is choosen by request_path
# static rewrite - home page
RewriteCond %{HTTP_COOKIE} !artio_mturbo=.*
RewriteCond %{REQUEST_URI} ^/magento/default(/|)$
RewriteCond %{QUERY_STRING} !.+
RewriteCond /var/www/var/turbocache/default.html -f
RewriteRule .* var/turbocache/default.html [L]
# static rewrite - other pages
RewriteCond %{HTTP_COOKIE} !artio_mturbo=.*
RewriteCond %{REQUEST_URI} ^/magento/default/(.*)\.html$ [NC]
RewriteCond %{QUERY_STRING} !.+
RewriteCond /var/www/var/turbocache/magento/default/%1.html -f
RewriteRule .* var/turbocache/magento/default/%1.html [L]
#cookie
RewriteCond %{HTTP_COOKIE} !artio_mturbo=.*
RewriteCond %{REQUEST_URI} ^/magento/$
RewriteCond %{QUERY_STRING} !.+
RewriteCond /var/www/var/turbocache/default.html -f
RewriteRule .* var/turbocache/default.html [L]
# rules for default storeview
# static rewrite - home page
RewriteCond %{HTTP_COOKIE} !artio_mturbo=.*
RewriteCond %{REQUEST_URI} /magento/(.*)\.html$ [NC]
RewriteCond %{QUERY_STRING} !.+
RewriteCond /var/www/var/turbocache/magento/default/%1.html -f
RewriteRule .* var/turbocache/magento/default/%1.html [L]
Thanks so far!
if ($http_cookie ~ "store=default"){
set $rule_0 1$rule_0;
}
if ($http_cookie !~ "artio_mturbo=.*"){
set $rule_0 2$rule_0;
}
if ($uri ~ "^/magento/$"){
set $rule_0 3$rule_0;
}
if ($args !~ ".+"){
set $rule_0 4$rule_0;
}
if (-f /var/ww/var/turbocache/default.html){
set $rule_0 5$rule_0;
}
if ($rule_0 = "54321"){
rewrite /.* /var/turbocache/default.html last;
}
if ($http_cookie ~ "store=default"){
set $rule_1 1$rule_1;
}
if ($http_cookie !~ "artio_mturbo=.*"){
set $rule_1 2$rule_1;
}
if ($uri ~* "/magento/(.*).html$"){
set $rule_1 3$rule_1;
}
if ($args !~ ".+"){
set $rule_1 4$rule_1;
}
if (-f /var/www/var/turbocache/magento/default/%1.html){
set $rule_1 5$rule_1;
set $bref_1 $1;
}
if ($rule_1 = "54321"){
rewrite /.* /var/turbocache/magento/default/$bref_1.html last;
}
if ($http_cookie !~ "artio_mturbo=.*"){
set $rule_2 1$rule_2;
}
if ($uri ~ "^/magento/default(/|)$"){
set $rule_2 2$rule_2;
}
if ($args !~ ".+"){
set $rule_2 3$rule_2;
}
if (-f /var/www/var/turbocache/default.html){
set $rule_2 4$rule_2;
}
if ($rule_2 = "4321"){
rewrite /.* /var/turbocache/default.html last;
}
if ($http_cookie !~ "artio_mturbo=.*"){
set $rule_3 1$rule_3;
}
if ($uri ~* "^/magento/default/(.*).html$"){
set $rule_3 2$rule_3;
}
if ($args !~ ".+"){
set $rule_3 3$rule_3;
}
if (-f /var/www/var/turbocache/magento/default/%1.html){
set $rule_3 4$rule_3;
set $bref_1 $1;
}
if ($rule_3 = "4321"){
rewrite /.* /var/turbocache/magento/default/$bref_1.html last;
}
if ($http_cookie !~ "artio_mturbo=.*"){
set $rule_4 1$rule_4;
}
if ($uri ~ "^/magento/$"){
set $rule_4 2$rule_4;
}
if ($args !~ ".+"){
set $rule_4 3$rule_4;
}
if (-f /var/www/var/turbocache/default.html){
set $rule_4 4$rule_4;
}
if ($rule_4 = "4321"){
rewrite /.* /var/turbocache/default.html last;
}
if ($http_cookie !~ "artio_mturbo=.*"){
set $rule_5 1$rule_5;
}
if ($uri ~* "/magento/(.*).html$"){
set $rule_5 2$rule_5;
}
if ($args !~ ".+"){
set $rule_5 3$rule_5;
}
if (-f /var/www/var/turbocache/magento/default/%1.html){
set $rule_5 4$rule_5;
set $bref_1 $1;
}
if ($rule_5 = "4321"){
rewrite /.* /var/turbocache/magento/default/$bref_1.html last;
}
Hope it will work ,good luck。
Here is a converter that give you this from your htaccess. This may give you a solid first base. After that, if you have further question please feel free to write them.