Htacces to nginx (crowdphp) - .htaccess

Can someone explain me how can i rewrite this to make it work with php-fpm (vestacp)?
It's a CrowdPHP script. Atm we need to move to php-fpm server, but... seems it is impossible to make a proper nginx template for this. I've tried to use some online converters, but nothing works.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^project\/(.*)-id-([0-9]+)\.html$ index.php?ctl=deal&act=show&id=$2 [QSA,L]
RewriteRule ^project\/updates\/(.\d)\/$ index.php?ctl=deal&act=update&id=$1 [QSA,L]
RewriteRule ^project\/comments\/(.\d)\/$ index.php?ctl=deal&act=comment&id=$1 [QSA,L]
RewriteRule ^user\/(.\d)\/$ index.php?ctl=home&id=$1 [QSA,L]
RewriteRule ^user\/(.\d)\/funded\/$ index.php?ctl=home&act=support&id=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#Custom Rerites
#RewriteRule ^projects-we-like.html$ index.php?ctl=article&id=77
RewriteRule ^article_cate$ index.php\?ctl=article_cate [QSA,L]
RewriteRule ^(about|Help|events|media|about-us|how-to)$ index.php\?ctl=article_cate&bs=$1 [QSA,L]
RewriteRule ^(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ index.php\?ctl=article&id=$2 [QSA,L]
RewriteRule ^([^\.&]*)\.html$ index.php\?rewrite_param=$1 [QSA,L]
</IfModule>

Use this
# nginx configuration location /project { rewrite ^/project\/(.*)-id-([0-9]+)\.html$ /index.php?ctl=deal&act=show&id=$2 break; rewrite ^/project\/updates\/(.\d)\/$ /index.php?ctl=deal&act=update&id=$1 break; rewrite ^/project\/comments\/(.\d)\/$ /index.php?ctl=deal&act=comment&id=$1 break; } location /user { rewrite ^/user\/(.\d)\/$ /index.php?ctl=home&id=$1 break; rewrite ^/user\/(.\d)\/funded\/$ /index.php?ctl=home&act=support&id=$1 break; } location / { if (!-e $request_filename){ rewrite ^/article_cate$ /index.php\?ctl=article_cate break; } rewrite ^/([^\.&]*)\.html$ /index.php\?rewrite_param=$1 break; } location /about { rewrite ^/(about|Help|events|media|about-us|how-to)$ /index.php\?ctl=article_cate&bs=$1 break; rewrite ^/(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ /index.php\?ctl=article&id=$2 break; } location /Help { rewrite ^/(about|Help|events|media|about-us|how-to)$ /index.php\?ctl=article_cate&bs=$1 break; rewrite ^/(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ /index.php\?ctl=article&id=$2 break; } location /events { rewrite ^/(about|Help|events|media|about-us|how-to)$ /index.php\?ctl=article_cate&bs=$1 break; rewrite ^/(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ /index.php\?ctl=article&id=$2 break; } location /media { rewrite ^/(about|Help|events|media|about-us|how-to)$ /index.php\?ctl=article_cate&bs=$1 break; rewrite ^/(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ /index.php\?ctl=article&id=$2 break; } location /about-us { rewrite ^/(about|Help|events|media|about-us|how-to)$ /index.php\?ctl=article_cate&bs=$1 break; rewrite ^/(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ /index.php\?ctl=article&id=$2 break; } location /how-to { rewrite ^/(about|Help|events|media|about-us|how-to)$ /index.php\?ctl=article_cate&bs=$1 break; rewrite ^/(about|Help|events|media|about-us|how-to)/([^\.&]*)\.html$ /index.php\?ctl=article&id=$2 break; }

Related

Avoid redirect chain in NginX when several conditions/rules in action?

Sorry, I'm really stupid in things I have to do, so may be this question is stupid to.
I moving my site from Apache to NginX. So I must to transfer .htacess redirects to NginX conf. So, I found a good converter and single rules works fine. But as soon as I try to implement several, the shit happens. For example, two steps:
1. nonWWW -> WWW
2. remove index.php from URI
in .htaccess it works fine as mysite.com/index.php?p=123 -> (301) www.mysite.com/?p=123 , at once, one redirect.
but in NgonX I did the same rules and it works as a chain:
site.ru/index.php?p=123 -> (301) site.ru/?p=123 -> (301) www.site.ru/?p=123 this is completely incorrect as I assume.
I hope that I just stupid and skip some standard and simple procedure like "putting in brackets" some set of conditions and rules to avoid chain... Please advise me what's wrong? Or my way is only to forget about NginX.
How I did this example:
# non-WWW to WWW
if ($http_host !~ "^$"){
set $rule_0 1$rule_0;
}
if ($http_host !~* "^www."){
set $rule_0 2$rule_0;
}
if ($http_host ~ "(.+)$"){
set $rule_0 3$rule_0;
set $bref_1 $1;
}
if ($rule_0 = "321"){
rewrite ^/(.*) http://www.$bref_1/$1 permanent;
}
# remove index.php string from URL
if ($uri ~ "^/index.php$"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite /. / permanent;
}
This above doesn't work at all, after nonWWW -> WWW it stops and index.php removal is not working.
# non-WWW to WWW
if ($http_host !~ "^$"){
set $rule_0 1$rule_0;
}
if ($http_host !~* "^www."){
set $rule_0 2$rule_0;
}
if ($http_host ~ "(.+)$"){
set $rule_0 3$rule_0;
set $bref_1 $1;
}
if ($rule_0 = "321"){
rewrite ^/(.*) http://www.$bref_1/$1 permanent;
}
# remove index.php string from URL
if ($uri ~ "^/index.php$"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite /. / permanent;
}
This works but as a chain...
Redirect non-www to www. Suggested by many sources (inc. official wiki) way - different servers with redirect in one:
server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
server_name www.example.com;
....
}
Index removal
server {
server_name www.example.com;
....
location / {
rewrite ^/index\.php$ / permanent;
....
}
}

Nginx configuration file

I'm quite new to Nginx and I'm testing out if this is the way to go for me in the future. On my server there are a couple of websites. I managed to set up Nginx correctly. However, on the old setup there was a .htaccess file, which made sure all the urls worked correctly.
This is the .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^verzekeringen/([a-z]+)$ products/product/index.php?page=$1 [L]
RewriteRule ^hypotheken/([a-z]+)$ products/product/index.php?page=$1 [L]
RewriteRule ^verzekeringen$ products/index.php?type=1 [L]
RewriteRule ^hypotheken$ products/index.php?type=2 [L]
An online converter, converted this to:
# nginx configuration
location /verzekeringen {
rewrite ^/verzekeringen/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location /hypotheken {
rewrite ^/hypotheken/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location = /verzekeringen {
rewrite ^(.*)$ /products/index.php?type=1 break;
}
location = /hypotheken {
rewrite ^(.*)$ /products/index.php?type=2 break;
}
In the standard configuration file, there is this part, where I think it should be. The problem however, is that I don't really know how to incorporate this into the file.
server {
server_name testwebsite.nl www.testwebsite.nl;
root /var/www/testwebsite.nl/htdocs;
location ~ \.php$ {
root /var/www/testwebsite.nl/htdocs;
fastcgi_pass ***.*.*.*:****;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You can actually paste the generated rules as is anywhere before ~ \.php$ rule.
you can also try this folded also :
server {
server_name testwebsite.nl www.testwebsite.nl;
root /var/www/testwebsite.nl/htdocs;
location /verzekeringen {
rewrite ^/verzekeringen/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location /hypotheken {
rewrite ^/hypotheken/([a-z]+)$ /products/product/index.php?page=$1 break;
}
location = /verzekeringen {
rewrite ^(.*)$ /products/index.php?type=1 break;
}
location = /hypotheken {
rewrite ^(.*)$ /products/index.php?type=2 break;
}
location ~ \.php$ {
root /var/www/testwebsite.nl/htdocs;
fastcgi_pass ***.*.*.*:****;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
See directive location and rewrite.
location ~ ^/verzekeringen$ {
rewrite ^(.*)$ /products/index.php?type=2 last;
}
location ~ ^/hypotheken$ {
rewrite ^(.*)$ /products/index.php?type=1 last;
}
location ~ /verzekeringen/[a-z]+$ {
rewrite ^/verzekeringen/(.*)$ /products/product/index.php?page=$1 last;
}
location ~ /hypotheken/[a-z]+$ {
rewrite ^/hypotheken/([a-z]+)$ /products/product/index.php?page=$1 last;
}

301 redirect is not removing the original paths in NGINX

I was trying to redirect a particular URL to a new URL using rewrite | 301 redirect catered by NGINX.
if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
rewrite ^.*$ playlist/all/ redirect;
}
If my request URI is http://servername.com/playlist/show/531a5aaefd3401705c000a32, then it should redirect to http://servername.com/playlist/all/
But what's happening right now is that it the original paths from old URL are still there:
http://servername.com/playlist/show/playlist/all/
The /playlist/show/ slashes/paths are still there, so it's redirecting to the wrong URI.
I'm pretty new in handling NGINX. Please bear with me. Thanks!
EDITS:
Here's my conf to be clearer:
server {
listen 80;
server_name site.servername.com;
server_tokens off;
charset utf-8;
root /home/site/stm/public;
location / {
index index.php;
# auth_basic "Restricted";
# auth_basic_user_file /home/site/stm/.htpasswd;
}
location = /favicon.ico {
access_log off;
log_not_found off;
empty_gif;
expires 30m;
}
if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
rewrite ^.*$ playlist/all/ redirect;
}
if ($request_uri = "/favicon.ico") {
rewrite ^.*$ /favicon.ico last;
}
if ($request_uri !~* ^/(?:static|favicon.ico)) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
You're doing it wrong: http://wiki.nginx.org/Pitfalls
You should never use if ($uri or if ($request_uri because it's just a very bad method of making location.
location =/playlist/show/531a5aaefd3401705c000a32 {
return 302 /playlist/all/;
}
Simple, fast, no regexps.
Reference to study:
http://nginx.org/r/location
Converting rewrite rules
How nginx processes a request

Url Rewriting Issue in Yii

I want to rewrite my yii application urls ,i just doen it and it works fine in my local machine . but when i move to server it is not working and shows a 500 Internal Server error
My current url is like this
cvdb.example.com/index.php/list/index
i want to rewrite it as
cvdb.example.com/list/index
Here is my code in config/main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
And here is my .htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Here what i get when i put var_dumb($_SERVER)
array(39) {
["DOCUMENT_ROOT"]=>
string(29) "/home/bridge/public_html/cvdb"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["HTTP_ACCEPT"]=>
string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
["HTTP_ACCEPT_LANGUAGE"]=>
string(14) "en-US,en;q=0.5"
["HTTP_CONNECTION"]=>
string(5) "close"
["HTTP_COOKIE"]=>
string(339) "fbm_1=base_domain=.cvdb.example.com; PHPSESSID=a6b697793d5abf01012435cf868a2f13; 52862fa7c29cebef614e5f38c01514c2=5571bdfdfdfdfdfdfb1ed61d4cae29f6145035a37a9d4ee45e83a%3A4%3A%7Bi%3A0%3Bs%3A1%3A%221%22%3Bi%3A1%3Bs%3A13%3A%22Administrator%22%3Bi%3A2%3Bi%3A31536000%3Bi%3A3%3Ba%3A1%3A%7Bs%3A4%3A%22role%22%3Bs%3A2%3A%2210%22%3B%7D%7D"
["HTTP_HOST"]=>
string(23) "cvdb.example.com"
["HTTP_USER_AGENT"]=>
string(72) "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"
["HTTP_X_FORWARDED_FOR"]=>
string(14) "220.336.5.3"
["HTTP_X_FORWARDED_HOST"]=>
string(23) "cvdb.example.com"
["HTTP_X_FORWARDED_SERVER"]=>
string(23) "cvdb.example.com"
["HTTP_X_REAL_IP"]=>
string(14) "220.336.5.3"
["PATH"]=>
string(13) "/bin:/usr/bin"
["PHPRC"]=>
string(13) "/home/bridge/"
["QUERY_STRING"]=>
string(0) ""
["REDIRECT_PHPRC"]=>
string(13) "/home/bridge/"
["REDIRECT_STATUS"]=>
string(3) "200"
["REDIRECT_UNIQUE_ID"]=>
string(24) "UtTWkTIfkzwAAddfdFUWCbwAAAAD"
["REDIRECT_URL"]=>
string(11) "/list/index"
["REMOTE_ADDR"]=>
string(14) "220.227.90.185"
["REMOTE_PORT"]=>
string(5) "43681"
["REQUEST_METHOD"]=>
string(3) "GET"
["REQUEST_URI"]=>
string(11) "/list/index"
["SCRIPT_FILENAME"]=>
string(39) "/home/bridge/public_html/cvdb/index.php"
["SCRIPT_NAME"]=>
string(10) "/index.php"
["SERVER_ADDR"]=>
string(12) "50.31.147.60"
["SERVER_ADMIN"]=>
string(33) "webmaster#cvdb.example.com"
["SERVER_NAME"]=>
string(23) "cvdb.example.com"
["SERVER_PORT"]=>
string(2) "80"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.0"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(156) "Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_python/3.3.1 Python/2.4.3 mod_jk/1.2.37 mod_bwlimited/1.4 mod_perl/2.0.6 Perl/v5.8.8"
["UNIQUE_ID"]=>
string(24) "UtTWkTIfkzwAAFUWCbwAAAAD"
["ORIG_PATH_INFO"]=>
string(11) "/list/index"
["ORIG_PATH_TRANSLATED"]=>
string(39) "/home/bridge/public_html/cvdb/index.php"
["PHP_SELF"]=>
string(10) "/index.php"
["REQUEST_TIME"]=>
int(1389680273)
["argv"]=>
array(0) {
}
["argc"]=>
int(0)
}
try this
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Don't perform redirects for files and directories that exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For everything else, redirect to index.php:
RewriteRule . index.php
</ifModule>
Or just try removing this line simply from your .htaccess file
RewriteBase /

nginx configuration for this .htaccess rule

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|png|gif)$ [NC]
RewriteCond %{REMOTE_ADDR} !^(127.0.0.1|XXX.254.185.XXX)$ [NC]
RewriteCond %{REMOTE_ADDR} !^66.6.(32|33|36|44|45|46). [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?(www.mydomain.org|pinterest.com|tumblr.com|facebook.com|plus.google.com|twitter.com) [NC]
RewriteCond %{HTTP_USER_AGENT} !(googlebot|Googlebot-Image|) [NC]
RewriteRule (.*) byrev-wp-picshield.php?key=ABCD&src=$1 [L]
</IfModule>
I have change this rule for ngnix through this website http://winginx.com/htaccess but it's not working..
Got same problem, and convert .htaccess rule with anilcetin.com.
And now, it's working.
THis is the sample
#HOTLINK PROTECTION START
if ($uri ~* ".(jpg|jpeg|png|gif)$"){
set $rule_0 1$rule_0;
}
if ($remote_addr !~* "^(127.0.0.1|xxx.xxx.xxx.xxx)$"){ #xxx.xxx.xxx.xx is your server ip
set $rule_0 2$rule_0;
}
if ($remote_addr !~* "^66.6.(32|33|36|44|45|46|40)."){
set $rule_0 3$rule_0;
}
if ($http_referer !~* "^http(s)?://(www.)?(www.findhomeideas.info|translate.google.com|translate.googleusercontent.com|www.microsofttranslator.com|pinterest.com|tumblr.com|facebook.com|plus.google|twitter.com|googleapis.com|googleusercontent.com|ytimg.com|gstatic.com)"){
set $rule_0 4$rule_0;
}
if ($http_user_agent !~* "(googlebot|msnbot|baiduspider|slurp|webcrawler|teoma|photon|facebookexternalhit|facebookplatform|pinterest|feedfetcher|ggpht)"){
set $rule_0 5$rule_0;
}
if ($http_user_agent !~* "(photon|smush.it|akamai|cloudfront|netdna|bitgravity|maxcdn|edgecast|limelight|tineye)"){
set $rule_0 6$rule_0;
}
if ($http_user_agent !~* "(developers|gstatic|googleapis|googleusercontent|google|ytimg)"){
set $rule_0 7$rule_0;
}
if ($rule_0 = "7654321"){
rewrite /(.*) /byrev-wp-picshield.php?key=xxxx-xxxx&src=$1 last; #xxx-xxx is your key
}
#HOTLINK PROTECTION START

Resources