Nginx, .Htaccess and try_files - .htaccess

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;

Related

.htacces to Nginx rewrite rule

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

How to convert htaccess to NGINX?

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

Convert htaccess rewrite rules to Nginx issues

I need assistance converting the following htaccess rules to Nginx.
AddHandler php5-script .php .html .html
AddType text/html .php .html .html
Opt0ions -Indexes
ErrorDocument 403 /index.php
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
#RewriteRule ^(.*)$ test.php/$1
RewriteRule ^(.*)$ index.php/$1
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
So far i have the following but dont know how to implement this within Nginx:
# nginx configuration
error_page 403 /index.php;
autoindex off;
location / {
if ($script_filename !~ "-f"){
rewrite ^(.*)$ /index.php/$1;
}
if ($http_host ~* "^www\.(.*)$"){
rewrite ^(.*)$ http://%1/$1 redirect;
}
}
The i had converted at http://winginx.com/en/htaccess
Is the converted rewrite rules above correct? And how can i implement these with Nginx?

mod-rewrite subdomain to path in primary domain

I have content from a different server on a subdomain that I would like to use as a subdirectory. Everything from sub.domain.com should be accessible to domain.com/sub I would like sub folders to work as well ex: sub.domain.com/about should be domain.com/sub/about
Here's my current .htaccess contents
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(sorry).* - [NC,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Any help appreciated. Thanks!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/sub%{REQUEST_URI} [R=301,L,NE]

.htaccess: Removing /index.php/foo

I have a site built with Contao. I just turned on URL rewriting and now index.php/foo returns a 404 error. I tried to rewrite /index.php/foo to /foo with
RewriteCond %{REQUEST_URI} ^/index\.php/
RewriteRule ^index.php/(.*) /$1 [R,L]
but it does not help.
Here's the rewrite part of the contao factory .htaccess with my additions in it's entirety (I edited out all the commented stuff to make it shorter):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/index\.php/ # My rule here
RewriteRule ^index.php/(.*) /$1 [R,L] # My rule here
<FilesMatch "\.(png|gif|jpe?g|js|css|ico|php|xml|csv|txt|gz|swf|flv|eot|woff|svg|ttf|htm)$">
RewriteEngine Off
</FilesMatch>
RewriteRule .*\.html$ index.php [L]
RewriteRule ^[a-z]{2}/$ index.php [L]
RewriteRule ^([a-z]{2})$ $1/ [R=301,L]
</IfModule>
What am I not getting here?
Never mind. I found the answer.
<FilesMatch "\.(png|gif|jpe?g|js|css|ico|THIS HERE CAUSED IT: --> ***php***|xml|csv|txt|gz|swf|flv|eot|woff|svg|ttf|htm)$">
RewriteEngine Off
</FilesMatch>

Resources