Please, tell me how to convert htaccess to NGINX on this example.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico)(\?|$) - [L,NC,R=404]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Apache makes things look a bit complex. But same config in Nginx is very simple
root /var/www/html;
location ~* \.(js|css|jpeg|jpg|gif|png|ico)(\?|$) {
try_files $uri =404;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php {
# <your fastcgi php handling config>
}
Related
I need some help with a conversion to nginx for this .htaccess rewrite rule.
RewriteEngine On
Options +FollowSymLinks
Options -Indexes
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]
Your .htaccess is a standard front controller pattern, e.g. you have a single bootstrap PHP file which handles requests for SEO friendly URLs.
In NGINX, the standard practice is using try_files directive for that pattern, like so:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
The above corresponds to the bottom 3 lines of your config:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]
The topmost lines do not need converting to NGINX, because those are already the default in NGINX:
rewrite module is enabled
symbolic links are followed
directory listing is off
Code that I'm using in .htaccess:
RewriteEngine On
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
I want to rewrite it to change all links to https://
When I add
RewriteEngine On
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteRule ^(.*)$ https://localhost/$1 [L,R=301]
Gives me error, too many redirects.
Or when try to change
RewriteEngine On
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
RewriteRule ^(.*)$ https://localhost/ [QSA,L]
Too many redirects too.
I'm also using apache with nginx. nginx code (maybe possible to do through nginx?)
server {
listen 127.0.0.1:80;
server_name localhost;
error_log /var/log/httpd/domains/localhost.error.log error;
location / {
proxy_pass http://127.0.0.1:8080;
location ~* ^.+\.(jpeg|jpg|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|mkv|mov|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {
root /home/admin/web/localhost/public_html;
access_log /var/log/httpd/domains/localhost.log combined;
access_log /var/log/httpd/domains/localhost.bytes bytes;
expires 4h;
try_files $uri #fallback;
}
}
location /error/ {
alias /home/admin/web/localhost/document_errors/;
}
location #fallback {
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include /home/admin/conf/web/nginx.localhost.conf*;
}
For Apache rewrite, you can use %{HTTPS} to check if it's HTTPS URL.
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Of course you can do it with nginx as well.
Since http accesses port 80 and https accesses port 443 by default, you can just setup a redirect to https url in your port 80 server block.
Something like this.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
ssl on;
# The rest of your config
}
I have a .htaccess file that I'm having trouble converting.
content of the .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and other folders,
RewriteCond $1 !^(index\.php|login|images|img|js|css|public|data|simplepie|application|system|cache)
RewriteRule ^(.*)$ index.php/$1 [L]
Nginx:
location / {
try_files $uri $uri/ index.php$is_args$args;
rewrite ^system.* index.php/$1 break;
if (!-e $request_filename){
rewrite !^(index\\.php|login|images|img|js|css|public|data|simplepie|application|system|cache) index.php/$1 break;
}
Any help will be appreciated.
Thanks
I need to convert this htaccess file into try_files for nginx but I cannot do it for the life of me. Can someone please help?
Options +FollowSymlinks
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^img-(.*)\.html img.php?id=$1 [L]
RewriteRule ^slide-(.*)\.html slider.php?id=$1 [L]
RewriteRule ^page-(.*)\.html page.php?name=$1 [L]
RewriteRule ^contact\.html$ contact.php [QSA,L,NC]
You don't need try_files here:
rewrite ^/img-(.*).html$ /img.php?id=$1 last;
rewrite ^/slide-(.*).html$ /slider.php?id=$1 last;
rewrite ^/page-(.*).html$ /page.php?name=$1 last;
rewrite ^/contact.html$ /contact.php last;
My htaccess code:
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
In nginx config I've tried this but it didn't work
location /mysite{
try_files $uri $uri/ /mysite/index.php$is_args$args;
}
Anyone can help me, tks so much.
p/s: I've tried http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ and http://winginx.com/htaccess, but both of them can't help