.htaccess to nginx rewrite rules - .htaccess

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;
}
}

Related

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.

nginx rewirte in two different folder (folder + sub folder different rewirte)

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;
}

.htaccess to nginx conf

I have this .htaccess rewrite rules
AddDefaultCharset UTF-8
RewriteEngine On
RewriteRule ^(administrator) - [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
and the nginx version rewrite rules
charset utf-8;
rewrite ^/$ /public/ last;
rewrite /(.*) /public/$1 last;
location ~* ^/(administrator) {
break;
}
The current .htaccess redirect all requests to /public folder except /administrator request.
With the nginx rules the [domain.tld/rss.php not working] [domain.tld/administrator working] [domain.tld working] [File not found.]
My application stracture is
.
..
index.php [require public/index.php]
administrator/index.php
public/index.php
public/rss.php
public/css
public/js
Sepcifiy an index file : index index.php.
By the way you should put rewrite in locations to avoid the test for every request. It's also better for readability/maitainability.
server {
server_name domain.tld;
root /path/to/root;
location ~ /(administrator|public) {
index index.php;
...
}
location / {
rewrite ^/$ /public/ last;
rewrite ^(.*)$ /public/$1 last;
}
}

Convert .htaccess to nginx rewrite rule

I'm attempting to convert the following (PHPVibe) .htaccess rule to gninx:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^embed/([^/]*)/$ /embed.php?id=$1 [L]
RewriteRule ^feed(.*)$ feed.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?rp=$1 [L]
</IfModule>
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
This is what I have so far:
rewrite ^/embed/([^/]*)/$ /embed.php?id=$1 last;
rewrite ^/feed(.*)$ /feed.php last;
if (!-f $request_filename){
set $rule_2 1$rule_2;
}
if (!-d $request_filename){
set $rule_2 2$rule_2;
}
if ($rule_2 = "21"){
rewrite ^/(.*)/?$ /index.php?rp=$1 last;
}
The above worked for the most part, except for the fact that it is returning 404 on my non mod_rewrite dependent pages.
Try inserting the following code before your other RewriteRules
RewriteRule ^moderator - [NC]
It should cause the rewrite engine to skip over any URLs that begin with /moderator/
location / {
try_files $uri $uri/ #index;
}
location /embed/ {
rewrite ^/embed/([^/]+)/$ /embed.php?id=$1;
}
location /feed {
rewrite ^ /feed.php;
}
location #index {
rewrite ^/(.*)/?$ /index.php?rp=$1;
}
location ~ \.php$ {
# fastcgi directives
}
So, after some tweaking, I've finally figured out the proper rewrite directives for nginx. In case anyone is wondering.
rewrite ^/embed/([^/]*)/$ /embed.php?id=$1 last;
rewrite ^/feed(.*)$ /feed.php last;
if (!-e $request_filename)
{
rewrite ^/(.*)/?$ /index.php?rp=$1 last;
}

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