Rewrite Results in NGINX - .htaccess

The RewriteRule written in a CentOS environment doesn't work with NGINX environment even after conversion.
Can anyone guide me, what syntax needs to be followed in an NGINX environment for Drupal rewriting rules?

For URL rewrites in Drupal, please put the following in your nginx vhost configuration within the location / section of the server section:
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
break;
}
As described here: https://www.drupal.org/node/110224

Related

Rewrite rule on nginx server not applied

I'm setting up an nginx server this time and wanted to implement my .htaccess from my old apache server.
<IfModule mod_rewrite.c>
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^proc/?$ process.php [NC,L]
</IfModule>
Which describes the following behaviour:
If I browse to http://server.io/proc/, then it should execute process.php , and /proc/ is not a folder!
Nice to have
Also it would be nice, if a direct execution of process.php is disabled - so only via /proc/ allowed.
What did I try already?
I've tried to input in my http.conf and default.conf inside the server{...}:
rewrite ^proc/?$ process.php last;
However thank you guys in advance.
Best Regards
finally after searching the web - I found a simple solution for my first problem.
Solution:
In my http.conf create this inside server{ ... }:
location /proc/ {
rewrite ^/proc/$ /process.php;
}
So, now when I call http://server.io/proc/ process.php will be executed.

.htaccess rewrite cond and rule to nginx config

I am trying to convert this apache .htaccess rule to something nginx can use.
This .htaccess is a part of this tutorial to make a php REST api. ( http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/ )
The idea of this .htaccess file is to make a REST get/post request to "task_manager/v1/register/"
and it redirects you to "task_manager/v1/index.php"
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
I've tried one idea from stackoverflow, one from winginx, and I tried writing one myself and they all sort of flop.
Nginx config:
something I tried and failed so I commented it out.
rewrite ^/task_manager/v1/(/)$ /task_manager/v1/index.php redirect;
something I saw in stackexchange and didn't help, so I commented it out
rewrite ^task_manager/api/v1/([^/]+)/([^/]+)/?$ task_manager/api/v1/index.php?class=$1&method=$2? last;
was recommended to use winginx, didn't work. :c
location / {
if (!-e $request_filename){
rewrite ^(.*)$task_manager/v1/index.php break;
}
}
Please advise me.
phew. Good thing I was only pretending to be dumb.
anyways, so yeah I was trying to make that android REST API work in nginx.
I took a look at another post ( nginx configuration for a RESTful API )
and basically I just needed to change a few things.
original:
rewrite ^/api/v1/([^/]+)/([^/]+)/?$ /apiv1.php?class=$1&method=$2? last;
Edit:
rewrite ^/task_manager/v1/([^/]+)/?$ /task_manager/v1/index.php?method=$2? last;
It seems to work for this specific tutorial. I ran into a different problem with php5-fpm but at least when I use REST Client on chrome it either gives me a 500 error (logs show something in my config isn't parsed correctly) or it will tell me "no there are no users"/"no you are not logged in" but at least this part is solved.

301 is not working on nginx server

A magento website transferred to Nginx and my 301 redirects are not working here.
Previous URL:
www.domain.com/store/food/two-year-supply-of-glide-r-chow-glide-a-mins.html
New URL:
www.domain.com//two-year-supply-of-glide-r-chow-glide-a-mins.html
Initially my .htaccess was
Redirect 301 /store/food/two-year-supply-of-glide-r-chow-glide-a-mins.html /two-year-supply-of-glide-r-chow-glide-a-mins.html
and now I have convert it to Nginx server format i.e.
location /store/food/two-year-supply-of-glide-r-chow-glide-a-mins.html {
rewrite ^(.*)$ /two-year-supply-of-glide-r-chow-glide-a-mins.html redirect;
}
Which is not working.
You conversion looks fine to me. sometime nginx server ignore .htacess file try with a new file named as nginx.conf and put your all htacess conversion in this.
Edit
# nginx configuration
location /store { rewrite ^/store/food/(.*)$ /$1 redirect; }
I think this might help you.

Nginx rewrite doesn't work when I try to match a file

I need to convert in Nginx these .htaccess rules, but when I try to target the filename and its extensions, Nginx doesn't match it. I really don't understand why of this behavior.
RewriteRule ^(.*?)(test_mod_rewrite) /$1media/plg_jchoptimize/assets/modrewrite.php?q=$2
RewriteRule ^(.*?)(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$5&type=$6&gz=$2&d=$3&i=$4
I converted these rules in Nginx:
location /media/plg_jchoptimize/assets/ {
rewrite ^/(test_mod_rewrite) /media/plg_jchoptimize/assets/modrewrite.php?q=$1;
rewrite ^/(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$4&type=$5&gz=$1&d=$2&i=$3;
}
The example URL is:
/media/plg_jchoptimize/assets/gz/30/0/d8604b25d503f1dcbb035ec731857648.css
What I'm wrong?

.htaccess file 404 error with parallel plesk server

I have my .htaccess file working in localhost. But its not working if i upload it in server. It throws me 404 error.
I am using Parallel Plesk 11.0.9 and i can't find conf file for the same on that. If anyone has any idea how to fix it or any workaround for url rewriting would be great help.
Anyway here's the code in htaccess:
RewriteEngine on
RewriteRule ^store/living/Hutches-Armoires-Side-tables-Coffee-tables-Entertainment-centers? store.php?store=Living
RewriteRule ^store/dining/sideboards-buffets-chairs-benches-Dining-table$ store.php?store=Dining
RewriteRule ^store/working/Bookshelves-Study-tables$ store.php?store=Working
RewriteRule ^store/accessories/Boxes-Photo-Frames-Mirror-Frames-Block-Stamps-and-Book-stands$ store.php?store=Accessories
RewriteRule ^store/hallway/Console-tables-Armoires$ store.php?store=Hallway
RewriteRule ^store/sleeping/Bed-Frames-Night-Stands-Dressers-Mirror-framesBed-Linens-Canopies-Curtains$ store.php?store=Sleeping
You may need to wrap your rewrite rules with:
<IfModule mod_rewrite.c>
...
</IfModule>
...probably a good idea anyway.
Or you could try putting your rules into a vhost.conf file in the conf directory immediately below the location of your httpdocs directory. For example on a Centos machine the web root might be
/var/www/vhosts/domain.com/httpdocs
and you should have a:
/var/www/vhosts/domain.com/conf
directory, this will contain a set of pregenerated Apache config files that Plesk creates. If there isn't already create a vhost.conf and add your rules between a set of
<Directory /var/www/vhosts/domain.com/httpdocs/ >
... your rules ...
</Directory>
Once you've created the vhost.conf file you will need to tell plesk about it with
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain domain.com
If you still can't get it to work you can add a log for mod_rewrite, see this relevant SO answer for details
Is your Plesk running IIS and supporting PHP via FastCGI or ISAPI? If that is the case, check whether URL Rewrite is installed and follow this guide to translate htaccess (for apache) into web.config (for IIS)

Resources