Convert htaccess to nginx, seek solution - .htaccess

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.

Related

No php extension and pretty url nginx config alternative

I just migrated my code from apache to nginx server.
What would be the alternative nginx config to my apache .htaccess.
What i use are rules for removing .php extension and pretty url rewrite.
RewriteEngine On
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]
Try this
map $uri $pretty_url {
~/(.*)$ $1;
}
server {
...
location / {
index index.php; # replace this to 'index index.php index.html index.htm;' if needed
try_files $uri $uri/ #extensionless-php;
}
location ~ \.php$ {
# default PHP-FPM handler here
...
}
location #extensionless-php {
if ( -f $document_root$uri.php ) {
rewrite ^ $uri.php last;
}
rewrite ^ /index.php?url=$pretty_url last;
}
}
Easy fix by changing one line. There's no need for extra blocks. You just need to change this one line in the /etc/nginx/nginx.conf or /etc/nginx/sites-available/your-site.com.
Change the location / directive try files to:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
Hope this works for you too :)

htaccess rules to nginx rules

I need help with reqrite htaccess rules to NGINX rules
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
if any one could help me if those rules can be worked as NGINX rules
Try this:
location / {
try_files $uri/ /$uri.php$is_args$args;
}
You will need the PHP scripts handler location block!
location ~ \.php$ {
# fastcgi or other backend configuration here
...
}

Migration failed from htaccess to nginx configuration

I'm in process of migration from .htaccess to nginx.conf file but after migration rules doesn't work as I expected.
This is content of .htaccess that I'm gonna migrato to nginx.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
And this is content of nginx.conf that I've migrated:
index index.php
location / {
rewrite ^(.*)$ /index.php?$1 break;
}
Please let me know what's wrong with this and how can I get nginx conf file works.
Regards. Yuming
Those .htaccess statements perform a similar function to the Nginx try_files directive.
Try:
location / {
try_files $uri $uri/ /index.php?$uri;
}
See this document for details.

ToroPHP Router in nginx

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
}

chive on nginx, htaccess

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

Resources