APACHE ro NGINX: how to translate rewrite rules? - apache

RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?apple=$1&bee=$2 [L]
that is my htaccess apache file
I'm a nginx newbie... no idea where and how to set this up

You can try with this one:
# nginx configuration
location / {
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ /index.php?apple=$1&bee=$2 break;
}
Also this is a really good tool that you can also check: https://winginx.com/en/htaccess

Related

Angular2 + Nginx Deep Linking/Routing Issue

I deployed an Angular2 application on my Apache web server and with the following .htaccess,
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html [L]
</IfModule>
and an additional configuration in index.html,
<base href="/applicationName/">
the application was able to load without any issue and there was no redirection problem.
I'm now trying to setup the same application in my Nginx server but I couldn't seem to make it work.
I understand that there is no .htaccess in Nginx, how do I convert the above .htaccess to work in Nginx main configuration?
Thanks a lot!
Nginx is actually quite a lot easier than apache in my opinion. One way to do it is to create a server block like this. This is of course if you have access to the configuration file of nginx on your server.
server {
listen 80;
server_name example.com;
root /path/to/your/index;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}

Apache ignore *.php files in rewrite directives but not other extensions

I'm trying to test some simple rewrite directives in .htaccess file on my test environment with Apache and PHP-FPM.
It seems that a simple rewrite rule works well for any type of file but not for *.php files.
This works :
RewriteEngine On
RewriteRule ^sandbox/video\.html?$ http://www.google.fr? [R,L]
But this not :
RewriteEngine On
RewriteRule ^sandbox/video\.php?$ http://www.google.fr? [R,L]
I run Apache 2.2 and PHP-FPM inside a VM on Debian
Could it be related ?
Thanks for your help
With PHP-FPM the .htaccess file is never read for php URL. You will need to add the rewrites to your main config.

Nginx rewrite equivalent of apache rewrite

I am trying to change the configuration on my new Nginx server so it matches my current Apache settings.
At the moment I am using this htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
I have found a converter that could "translate" this into the Nginx equivalent. I have tried the following:
location / {
if (!-e $request_filename){
rewrite ^/(.*) /index.php?url=$1 break;
}
try_files $uri $uri/ =404;
}
But when I try to set a url query like this http://domain.tld/something my php file gets returned and downloaded and that ain't supposed to happen.
What I expect to happen is when a url like http://domain.tld/something is entered it gets treated like http://domain.tld/index.php?url=something
Can someone tell me what I am doing wrong?
Unlike Apache, nginx doesn't come out-of-the-box ready to run PHP. You need to setup a handler to deal with php files, otherwise nginx will serve them up just like any regular file.
See: Nginx downloads php instead of running it
Also: review the NGINX documentation on using fast-cgi to run php.

how to turn URL to the new domain?

old domain link:
http://www.old.com/aa/bb/cc.html
i want when this old link to be visiting,turn to:
http://www.new.com/aa/bb/cc.html
how to do this?
BTW: nginx or apache
Create a .htaccess file similar to the following and place it to the old domain root directory.
RewriteEngine On
RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,QSA]
For nginx I go with this on account that it also helps with alias domains.
if ($host != 'www.new.com' ) {
rewrite ^/(.*)$ http://www.new.com/$1 permanent;
}

Simple rewrite rule for a nginx + apache2 with mod_wsgi

I'm stuck with this, my skills in the web servers area are poor...
I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:
=== /etc/nginx/sites-enabled/example.com ===:
http://dpaste.com/82638/
=== /etc/apache2/sites-enabled/example.com ===:
http://dpaste.com/hold/82645/
=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:
http://dpaste.com/82643/
In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That worked perfectly.
But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:
http://example.com/example.wsgi/
Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:
rewrite ^/(.*) http://example.com/$1 permanent;
but now I am getting what firefox calls a "circular loop"...
So who can I get this (I guess trivial) thing up?
Thanks in advance,
Hector
The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com
server {
listen 80;
server_name www.example.com;
rewrute ^/(.*) http://example.com/$1 permanent;
}
All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.