I tried the online converters and none of them worked.
How can I convert the below lines of htaccess to nginx rule. I was only able to convert one line from the below code. I don't even know whether its right or wrong.
My nginx Conf
server {
server_name example.com;
root /var/www/example.com;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
This is code I want to convert to nginx.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# cors
Header set Access-Control-Allow-Origin "*"
</IfModule>
# Hide a specific file
<Files .env>
Order allow,deny
Deny from all
</Files>
These are the only lines I was able to convert.
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
To
try_files $uri $uri/ /index.php;
Still I have doubt in the above nginx rule whether it should contain ?$args before the colon mark or not?
Kindly help me with this.
Please note that I used an AI to assist in this conversion. Curious to see how it did.
# cors
add_header Access-Control-Allow-Origin *;
# gzip
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# cache
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
# security
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "no-referrer";
add_header X-Permitted-Cross-Domain-Policies "none";
# php
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
Related
I have different problem with nginx rewrite.
In apache I use to htaccess file in root and sub directory.
Use in root folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?req=$1 [QSA,L]
</IfModule>
and in subfolder with cp:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ router.php?get=$1 [QSA,L]
</IfModule>
But when i try to configure nginx by this:
but what i try:
server {
listen xx.xx.xx.xx:80;
server_name mydomain.net www.mydomain.net;
root /home/admin/web/mydomain.net/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/mydomain.net.log combined;
access_log /var/log/nginx/domains/mydomain.net.bytes bytes;
error_log /var/log/nginx/domains/mydomain.net.error.log error;
#first block
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?req=$1 last;
}
}
#second block
location /cp {
if (!-e $request_filename){
rewrite ^(.+)$ /router.php?get=$1 last;
}
}
}
But when i Try download force php.
This problem not look like other problems i search many about this and I cant find the answer
when I remove second block rewrite work on root.
For location issue you should check this answer. For processing php you should configure php-fpm and add to nginx configuration php-processing location:
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/nginx/$fastcgi_script_name;
fastcgi_index index.php;
}
I have a Joomla website which I want to move from Apache server to Nginx. My website have 3 languages and all URLs begins with /en, /ua or /ru. SEF URLs are enabled and work perfectly at Apache.
Website folder have .htaccess file, where (as far as I understand) the most important directives are:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
I used http://winginx.com/en/htaccess converter, and placed its result into /etc/nginx/site-available/domain.com file:
index index.php index.html;
autoindex off;
location / {
if ($http_host ~* "^www.domain.com$") {
rewrite ^(.*)$ http://domain.com/$1 redirect;
}
if (-e $request_filename) {
rewrite ^/(.*) /index.php;
}
}
Then I've read a manual (https://docs.joomla.org/Enabling_Search_Engine_Friendly_%28SEF%29_URLs#Nginx), but nor try_files $uri $uri/ /index.php?q=$request_uri; nor expires 1d; try_files $uri $uri/ /index.php?$args; doesn't help me.
If I try to open website in my browser using directives from previous paragraph, I've got 504 (gateway timeout) error; error.log contains next error:
*1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 176.***.**.***, server: domain.com, request: "GET /en/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "domain.com"
If I don't use this and just use my directives which was converted from .htaccess, then I've got 500 (internal server) error and error.log contains next thing:
*1 rewrite or internal redirection cycle while internally redirecting to "/ru/", client: 176.***.**.***, server: domain.com, request: "GET /ru/ HTTP/1.1", host: "domain.com"
I try to google solutions a whole day, but I think I can't handle this without help of specialist :(
Also at Joomla control panel I've found that "SEF URLs work correctly at Apache and IIS7", and there is no info about Nginx. Though at docs.joomla.org there is a solution, but it doesn't work or I do something wrong =/
Please. Help!
I would like to convert this .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Into a nginx.conf file. So far my nginx.conf file looks like this:
server {
listen 80;
server_name _;
root /var/www/app;
index index.php;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:8080;
fastcgi_index index.php;
include fastcgi_params;
}
}
I am getting a 502 bad gateway error.
Correct your location for compatibility with your apache rewrites:
location / {
try_files $uri /index.php?$args;
}
And error 502 means that fpm-php (fastcgi) backend is down, but this is not nginx issue :)
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
}
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;
}
}