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
...
}
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 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 :)
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.
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 want to convert a simple .htaccess file (rules) to be used with nginx. I try to access an online tool after googleit but that website is down. So, if anyone cand help, i would appreciate. Thank you. Here is my .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Please, try these rules:
# Deny all files that started with dot
location ~ /\. {
deny all;
}
# Try $uri - is file, $uri/ - is dir, index.html - caching, $uri.html caching, index.php -last
location / {
try_files $uri $uri/ index.html $uri.html index.php;
}
Please, try these rules:
# nginx configuration
location ~ \.html$ {
}
location / {
if ($request_uri ~ "\..+$"){
rewrite ^/$ /index.html;
}
rewrite ^/([^.]+)$ /$1.html;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}