How will it look like in /etc/nginx/sites-available/default?
I use nginx/1.6.0
<Directory "c:/wamp/www/ProjectName">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
</Directory>
I've tried:
location /ProjectName {
try_files $uri $uri/ /ProjectName/index.php?r=$request_uri;
}
It almost seems to work but if I have are url with questions mark:
http://ProjectName.dk/Test/site/getPerson?Id=369
I get error?
The question is what else should be added to the nginx conf?
Found the solution:
try_files $uri $uri/ /ProjectName/index.php?r=$query_string;
Now it works with questions mark.
Related
Recently I moved to Nginx I fixed my all WordPress sites by adding a custom script
location / {
try_files $uri $uri/ /index.php?$args;
}
But now I noticed I had a custom script in the back end folder with its own .htaccess and it stopped working now.
I was using
Options -Multiviews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?x=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
</IfModule>
This .htaccess allows me to send https://example.com/apps/scripts/api/check_connection
trail directly into api.php file as a api.php?x=check_connection
Now In Nginx I tried
# Serves api
location /apps/scripts/api/ {
try_files $uri $uri/ /api.php?x=$args;
}
But the server is returning 404 for https://example.com/apps/scripts/api/check_connection please give me a solution to fix this.
I have no idea how Nginx configs work.
I tried https://winginx.com/en/htaccess and https://www.getpagespeed.com/apache-to-nginx to convert .htaccess to Nginx config but both websites' config does not work for me.
I will be really grateful to find the solution
I have this nginx setup on all my websites. If I change this too much, usually the website does not work. The problem now is that I have a website that I bought with an htaccess file. I tried to convert using online converters, but nothing works...
MY NGINX CONFIG
server {
server_name doutor.pt www.doutor.pt;
access_log /var/log/nginx/doutor.pt.access.log;
error_log /var/log/nginx/doutor.pt.error.log;
root /var/www/doutor.pt/htdocs;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
HTACCESS:
################################
# MAIN SETTINGS #
################################
# Remove index
Options -Indexes
# Set directory indexes
DirectoryIndex index.html index.php under-construction.html parking-place.html
################################
# APACHE REWRITES #
################################
RewriteEngine On
# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap-doctors-([0-9]+).xml$ index.php?page_name=sitemap&category=doctors&page=$1 [L,QSA]
# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap-doctors.xml$ index.php?page_name=sitemap&category=doctors&page=0 [L,QSA]
# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap.xml$ index.php?page_name=sitemap [L,QSA]
# Domain page parser for category
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/([0-9]+)-([^/]+)$ index.php?page_name=doctors&category=$1 [L,QSA]
# Domain page parser for doctor page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medico/([^/]+)$ index.php?page_name=medico&doctor_url=$1 [L,QSA]
# Domain page parser for secondary level pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ index.php?page_name=$2&page_category=$2 [L,QSA]
# Domain page parser for regular pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !sitemap$
RewriteRule ^([^/]+)$ index.php?page_name=$1 [L,QSA]
################################
# CUSTOM #
################################
Nginx is a beautiful thing and enables almost all the time to use a location block instead of a potentially hard to follow/understand/debug rewrite. This htaccess files converts easily and i've tested a bit of it's output (result) a little while back when i was asked if i could help on some other site.
As Richard's code shows, apache's weird negative logic -d and -f translate easily to a try_files $uri $uri/ in nginx where you check the request uri against web root path.
The directory indexing is disabled by default in nginx so one must enable it to use it... nothing to add is better than (again) apache's weird negative logic. Here's what the index would look like...
index index.php index.html under-construction.html parking-place.html;
Here's what the sitemap rewrite translate to, from my understanding:
# If the URI is bang on /sitemap.xml
location = /sitemap.xml {
try_files $uri $uri/ /index.php?page_name=sitemap;
}
# If the URI is bang on /sitemap-doctors.xml
location = /sitemap-doctors.xml {
try_files $uri $uri/ /index.php?page_name=sitemap&category=doctors&page=0;
}
# Enclosing this in the ~* /sitemap block isn't mandatory but it only
# helps creating cleaner, readable code while also making sure we contain
# all /sitemap*s requests thus taking care of the !sitemap directive.
location ~* /sitemap {
location ~ ^/sitemap-doctors-([0-9]+).xml {
try_files $uri $uri/ /index.php?page_name=sitemap&category=doctors&page=$1;
}
}
Following the same logic, the next one in line would look something like this:
# It seems we are only using the first capture group ( $1 ) here so the
# regex could be modified for something simpler but to make sure we are
# not catching stuff we don't want, leaving this precision will save troubles
location ^/category/([0-9]+)-([^/]+)$ {
try_files $uri $uri/ /index.php?page_name=doctors&category=$1;
}
Again, for more of the same...
# The ^ and $ are regex delimiters for start and end, so you know...
location ^/medico/([^/]+)$ {
try_files $uri $uri/ /index.php?page_name=medico&doctor_url=$1;
}
Now that we got rid of all precise stuff, it's time for some generic uri catching and passing to php...
I'm thinking this htaccess isn't proper... I would love to get an explanation..
This here sends the capture block ( $2 ) twice which in my opinion is really unusual and doesn't make sense. That being said, without seeing it in action or looking at the source code, it's a tricky one...
RewriteRule ^([^/]+)/([^/]+)$ index.php?page_name=$2&page_category=$2 [L,QSA]
a url of http://hungry.man/delicious/pizza would be seen to php as http://hungry.man/index.php?page_name=pizza&page_category=pizza without any reference to it being delicious...
For what's left i would be tempted to just use a #rewrite and write thoses as they are, without challenging my brain too much about it.
Hope it helps, Mat
It seems to me that the .htaccess file is trying to do something like this:
location / {
try_files $uri $uri/ #rewrite;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
location #rewrite {
rewrite ^/sitemap-doctors-([0-9]+).xml$ /index.php?page_name=sitemap&category=doctors&page=$1 last;
rewrite ^/sitemap-doctors.xml$ /index.php?page_name=sitemap&category=doctors&page=0 last;
rewrite ^/sitemap.xml$ /index.php?page_name=sitemap last;
rewrite ^/category/([0-9]+)-([^/]+)$ /index.php?page_name=doctors&category=$1 last;
rewrite ^/medico/([^/]+)$ /index.php?page_name=medico&doctor_url=$1 last;
rewrite ^/([^/]+)/([^/]+)$ /index.php?page_name=$2&page_category=$2 last;
rewrite ^/([^/]+)$ /index.php?page_name=$1 last;
# some default action???
return 404;
}
The main difference between rewrites in Apache and nginx, is that the latter requires the leading / on URIs. I have not implemented the RewriteCond %{REQUEST_URI} !sitemap$ rule or implemented a final default action. You will need to determine what works: return 404 or rewrite everything to /index.php.
I'm new to nginx and having a hard time trying to convert this htaccess file into readable nginx logic:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
The farthest I came up with was this:
location / {
try_files $uri $uri/ /index.php?uri=$args;
}
What am I doing wrong?
Thanks in advance
You need both URI and query string arguments, $uri and $args in nginx.
Try this:
location / {
try_files $uri $uri/ /index.php?uri=$uri&$args;
}
I am hosting ToroPHP in /api/. My /api/.htaccess was
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
This worked great. I converted this to the nginx equivalent which got me roughly this:
rewrite ^/api/(.*)$ /api/index.php/$1 last;
But this isn't working. What should the nginx equivalent be?
This question really belongs on http://serverfault.com, nonetheless:
Use try files
Rewriting isn't necessary, the desired results can be achieved more efficiently using try_files:
server {
...
location /api {
# try_files $uri $uri/ index.php$uri;
try_files $uri $uri/ index.php;
}
}
}
Note that it's not necessary to pass the url as an argument to index.php as it's already available as $_SERVER['REQUEST_URI'] (will require trivial modifications to index.php to work).
location /api {
try_files $uri $uri/ index.php$request_uri; # or /api/index.php$request_uri
}
Hello there im trying to setup chive on nginx, can someone help me convert htaccess for it ?
Options +FollowSymLinks
IndexIgnore */*
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php?__chive_rewrite_on=1&%{QUERY_STRING}
RewriteRule ^$ index.php?__chive_rewrite_on=1&%{QUERY_STRING}
</IfModule>
In case you installed chive in a subdirectory "chive" do the following:
location /chive/ {
try_files $uri chive/$uri/ /chive/index.php?q=$uri&$args;
}
without a subdir:
location / {
# ... existing options ... check twice!
try_files $uri $uri/ /index.php?q=$uri&$args;
}
instead of $args you could use $query_string, according to http://wiki.nginx.org/HttpCoreModule (It reads: "The same as $args except that this variable is readonly.")
Great project appeared since then
https://github.com/perusio/chive-nginx