Rewrite rule for nginx from Apache

Can anyone tell me how to rewrite this rules from apache:

RewriteEngine On
RewriteRule ^shorten/(.*)$ shorten.php?longurl=$1 [L]
RewriteRule ^([0-9a-zA-Z]{1,6})$ redirect.php?url=$1 [L]

to nginx?

Thanks!

Hello Robert,

Wednesday, November 18, 2009, 9:17:23 PM, you wrote:

Can anyone tell me how to rewrite this rules from apache:

RewriteEngine On
RewriteRule ^shorten/(.*)$ shorten.php?longurl=$1 [L]
RewriteRule ^([0-9a-zA-Z]{1,6})$ redirect.php?url=$1 [L]

to nginx?

Thanks!

location ~ ^/shorten/(?.*)$ {
root …;
fastcgi_pass …;
fastcgi_param SCRIPT_NAME /shorten.php;
fastcgi_param SCRIPT_FILENAME $document_root/shorten.php;
fastcgi_param QUERY_STRING longurl=$longurl;
}

location ~ “^/(?[0-9a-zA-Z]{1,6})$” {
root …;
fastcgi_pass …;
fastcgi_param SCRIPT_NAME /redirect.php;
fastcgi_param SCRIPT_FILENAME $document_root/redirect.php;
fastcgi_param QUERY_STRING url=$url;
}

Thx, can u also help me with this:

RewriteCond %{REQUEST_URI} /([0-9a-z]{6})$ [NC]
RewriteRule ^(.*) http://www.example.com/forward.php?%1 [L]

Thanks again!

Hello Robert,

Thursday, November 19, 2009, 4:28:17 PM, you wrote:

Thx, can u also help me with this:

RewriteCond %{REQUEST_URI} /([0-9a-z]{6})$ [NC]
RewriteRule ^(.*) http://www.example.com/forward.php?%1 [L]

Thanks again!

location ~ “/(?[0-9a-z]{6})$” {
rewrite ^ http://www.example.com/forward.php?$fwd last;
}

or

location ~ “^/(?[0-9a-z]{6})$” {
rewrite ^ http://www.example.com/forward.php?$fwd last;
}

Hey, thanks for the help, but Im not getting how do you actually do it.

For example I got a new rewrite rule from Apache

RewriteOptions MaxRedirects=1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9_-]+)$ redirect.php?alias=$1 [L]

and did this:

location ~ “^/(?([a-zA-Z0-9_-]))$” {
root $site_root;
fastcgi_pass unix:/var/tmp/php-fastcgi.socket;
fastcgi_param SCRIPT_NAME /redirect.php;
fastcgi_param SCRIPT_FILENAME $site_root/redirect.php;
fastcgi_param QUERY_STRING alias=$alias;
}

and it doesn’t work. Do you know why?

Please can you help me to convert this to nginx

RewriteEngine on

If you are having problems or are using VirtualDocumentRoot, uncomment

this line and set it to your directory.

RewriteBase /maindir/

RewriteOptions MaxRedirects=30
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^index(.)$ index.php
[QSA]
RewriteRule ^chat/.
pages.php
[QSA]
RewriteRule ^Songs(.)$
showcat.php?g=2 [QSA]
RewriteRule ^Jokes(.
)$
showcat.php?g=3 [QSA]
RewriteRule ^Ash3ar(.)$
showcat.php?g=4 [QSA]
RewriteRule ^iraq/.
showcat.php
[QSA]
RewriteRule ^Song/.* sounds.php
[QSA]
RewriteRule ^Joke/.* jokes.php
[QSA]
RewriteRule ^Poem/.* poems.php
[QSA]
RewriteRule ^link/.* advs.php
[QSA]
RewriteRule ^search/.* search.php
[QSA]
RewriteRule ^adv-conditions(.)$
ad.php?go=conditions [NC]
RewriteRule ^adv-payments(.
)$
ad.php?go=payments [NC]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.$ - [NC,L]
RewriteRule ^(?:(.
?)(?:/|$))(.*|$)$ $1.php?r=$2 [QSA]

SecFilterEngine Off SecFilterScanPOST Off mod_gzip_on No

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,197702#msg-197702

using http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ , I got
this:

#ignored: condition 0
#ignored: “-” thing used or unknown variable in regex/rew
if (-e $request_filename){
set $rule_1 1;
}
if ($request_filename ~ “-l”){
set $rule_1 1;
}
if (-d $request_filename){
set $rule_1 1;
}
if ($rule_1 = “1”){
#ignored: “-” thing used or unknown variable in regex/rew
}
rewrite ^/index(.)$ /;
rewrite ^/chat/.
/;
rewrite ^/Songs(.)$ /;
rewrite ^/Jokes(.
)$ /;
rewrite ^/Ash3ar(.)$ /;
rewrite ^/iraq/.
/;
rewrite ^/Song/.* /;
rewrite ^/Joke/.* /;
rewrite ^/Poem/.* /;
rewrite ^/link/.* /;
rewrite ^/search/.* /;
rewrite ^/adv-conditions(.)$ /;
rewrite ^/adv-payments(.
)$ /;
if (-e $request_filename){
set $rule_15 1;
}
if ($request_filename ~ “-l”){
set $rule_15 1;
}
if (-d $request_filename){
set $rule_15 1;
}
if ($rule_15 = “1”){
#ignored: “-” thing used or unknown variable in regex/rew
}
rewrite ^/(?:(.?)(?:/|$))(.|$)$ /$1.php?r=$2;


omega13a
Owner and Founder of UFT
http://www.fedtrek.com

;(

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,198081#msg-198081

well first of all thank you for you quick reply …
but i’ve tried this tool and it’s not working…
i there any way to know the basics of the ngnix???

other wise look at the rewrite model

rewrite ^/index(.)$ /;
rewrite ^/chat/.
/;
rewrite ^/Songs(.)$ /;
rewrite ^/Jokes(.
)$ /;
rewrite ^/Ash3ar(.)$ /;
rewrite ^/iraq/.
/;
rewrite ^/Song/.* /;
rewrite ^/Joke/.* /;
rewrite ^/Poem/.* /;
rewrite ^/link/.* /;
rewrite ^/search/.* /;
rewrite ^/adv-conditions(.)$ /;
rewrite ^/adv-payments(.
)$ /;

it haven’t consider the file file names … so i’m little bit confused
about that tool
and so it didn’t work

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,197717#msg-197717

First of all thank you very much mr Igor This is a great help for me
but as i’m new to nginx so i’m little bit careful
but i have afew more things to ask about

do i have to make any defination to these params

SCRIPT_FILENAME
maindir

which is used as
fastcgi_param SCRIPT_FILENAME /path/to/script/pages.php;
AND
location /maindir/chat/

Or they just have to be written as they are??

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,198889#msg-198889

On Sat, May 14, 2011 at 05:38:49PM -0400, the_traveller wrote:

;(

Rewrite rules are not configuration means. They are primitive
programming
language that is too hard to understand. For example, these

RewriteRule ^chat/.* pages.php [QSA]
RewriteRule ^Songs(.*)$ showcat.php?g=2 [QSA]

may be translated to

location /maindir/chat/ {
fastcgi_pass …
fastcgi_param SCRIPT_FILENAME /path/to/script/pages.php;
fastcgi_param QUERY_STRING $args;

}

location /maindir/Songs/ {
fastcgi_pass …
fastcgi_param SCRIPT_FILENAME /path/to/script/showcat.php;
fastcgi_param QUERY_STRING g=2$is_args$args;

}


Igor S.

Hello i am new to nginx and i need some help to convert my .htaccess, i
have tried some shit but that not work …

Options FollowSymLinks
RewriteEngine On

RewriteRule ^(.).php/img/(.)$ img/$2 [C]
RewriteRule ^/img/(.*)$ img/$2 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !^scriptsFtp.*$

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Thanks

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,199175#msg-199175

On Tue, May 17, 2011 at 06:51:33PM -0400, the_traveller wrote:

First of all thank you very much mr Igor This is a great help for me
but as i’m new to nginx so i’m little bit careful
but i have afew more things to ask about

do i have to make any defination to these params

SCRIPT_FILENAME
maindir

“/maindir/” is from
RewriteBase /maindir/

By default it is “/”, for example,

location /chat/

SCRIPT_FILENAME is FastCGI parameter which contains file system script
name.

which is used as
fastcgi_param SCRIPT_FILENAME /path/to/script/pages.php;
AND
location /maindir/chat/

Or they just have to be written as they are??


Igor S.

thx i gonna to try it tommorow :wink:

the system is used for subdomain with a wildcard, example
toto.domain.com show a mini website for toto and same things for 300
other users, the website is the same but the content depend of the name
of the subdomain, all is stored in mysql for images and such …

sorry for my english …

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,199607#msg-199607

Thanks that work really fine :wink:

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,199984#msg-199984

On Wed, May 18, 2011 at 10:52:39PM -0400, ltss wrote:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

It’s not easy to understand what these .htaccess should do.
Probably this:

root /path/to/files;

location / {
try_files $uri $uri/ /index.php?$args;
}

location /scriptsFtp {

}

location ~ ^(.).php/img/(.)$ {
alias /path/to/img/$2;
}

location ~ .php$ {
fastcgi_pass …

}


Igor S.

On Mon, May 23, 2011 at 01:18:30PM -0400, the_traveller wrote:

}

and the Original Apache rule is
RewriteRule ^Joke/.* jokes.php [QSA]

sposing that the script is in the public dir of the ftp
is my try right??

SCRIPT_FILENAME should point to a file, for example,
/usr/www/scripts/jokes.php.

and also is the three dots in the second line
fastcgi_pass …
and before the closing tag

}
is this is right or i have to remove them ??

thanks alot for ur help
and if i have removed them

dots means that you should add your specific paramters, that is,

 fastcgi_pass   backend:9000;

etc.


Igor S.

I know i’m alittle bit annoying

but can you show me how i must write the rule
the path of the script jokes.php is in the public_html dir
and it should talks the args like that
jokes.php?j=$1

so in Apache i used to make the rule like that
RewriteRule ^Joke/.* jokes.php [QSA]

but i alwayes have the sever error on all of my tries

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,200852#msg-200852

Hi mr Igor
I’m sory coz i toke alittle bit too long

so if i have to make any ngnix rule i have to make it like that by ex:

location /Joke/ {
fastcgi_pass …
fastcgi_param SCRIPT_FILENAME /jokes.php;
fastcgi_param QUERY_STRING $args;

}

and the Original Apache rule is
RewriteRule ^Joke/.* jokes.php [QSA]

sposing that the script is in the public dir of the ftp
is my try right??

and also is the three dots in the second line
fastcgi_pass …
and before the closing tag

}
is this is right or i have to remove them ??

thanks alot for ur help
and if i have removed them

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,23868,200161#msg-200161

On Wed, May 25, 2011 at 02:30:28PM -0400, the_traveller wrote:

I know i’m alittle bit annoying

but can you show me how i must write the rule
the path of the script jokes.php is in the public_html dir
and it should talks the args like that
jokes.php?j=$1

Where does this public_html directory reside ?
/usr/local/www, /home/user/, or anything else ?


Igor S.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs