Converting a few rules from .htacess to NGINX - .htaccess

I am trying to get a few .htaccess rules converted to NGINX, but it's not going very well. I have to crawl here and beg for assistance :(~ Paypal-beer to anyone who solves it.
I have tried some online converters, but the code looks messy, and I don't quite understand what it's doing. The rules I would like to have converted:
# Rewrite any calls to html|json|xml|atom|rss
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)\.(html|json|xml|atom|rss)$ $1/ [L]
# Rewrite any calls to /render to the image resizer
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]
# Rewrite routes to application index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]
Appreciate any help.

I believe it is sorted. Inside a NGINX server {} block, I included the following:
location / {
if (!-e $request_filename){
# Rewrite any calls to html|json|xml|atom|rss if a folder matching * exists
rewrite (.+)\.(html|json|xml|atom|rss)$ $1/ last;
# Rewrite any calls to /render to the X3 image resizer
rewrite ^/render/. /app/parsers/slir/ last;
# Rewrite routes to X3 application index.php if they are non-existent files/dirs
rewrite ^(.*)$ /index.php?$1 last;
}
}
https://gist.github.com/mjau-mjau/6dc1948284c90d167f51f1e566a8457b

Related

Convert HTACCESS mod_rewrite directives to nginx format?

I'm brand new to nginx and I am trying to convert the app I wrote over from Apache as I need the ability to serve a lot of clients at once without a lot of overhead!
I'm getting the hang of setting up nginx and FPM/FastCGI PHP but I can't wrap my head around nginx's rewrite format just yet. I know you have to write some simple script that goes in the server {} block in the nginx config but I'm not yet familiar with the syntax.
Could anyone with experience with both Apache and nginx help me convert this to nginx format? Thanks in advance!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^school\.dev$ [NC]
RewriteRule ^(.*)$ http://www.school.dev/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
Here is the nginx conversion for your htaccess.
# nginx configuration
location / {
if ($http_host ~* "^school\.dev$"){
rewrite ^(.*)$ http://www.school.dev/$1 redirect;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?$query_string break;
}
}

URL rewrite checks for static files then rewrites to index.php

OK, I'm pants at rewrite rules in .htaccess files!
My desired scenario is (using the URL http://doma.in/ as an example):
First check to see if an index.html file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php
To expand on my example, say we requested the URL http://doma.in/js/foobar.js:
First check to see if an foobar.js file exists in the /public/js sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=js%2Ffoobar.js
That would cover static files but I also need URLs like http://doma.in/foo:
First check to see if an foo file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=foo
And a URL http://doma.in/foo/bar:
We can assume the file foo/bar does not exists in the /public sub-dir as files can't be named like that.
So serve (rewrite to) index.php?controller=foo&action=bar
I'm sure if this complicated (for me) bit is covered then I can work query-strings into the occasion too; so http://doma.in/foo/bar/foo/bar would serve index.php?controller=foo&action=bar&querystring=foo%2Fbar.
I'd also like to make sure that a trailing slash is handled the same as if a trailing slash was omitted, for example: http://doma.in/foo/bar/foo/bar and http://doma.in/foo/bar/foo/bar/
I'll handle 404s from within my app as if the file did not exist, it would redirect to index.php which does exist - I'm happy with this unless you've a better solution :)
I really hope all this makes sense as I've been looking to find a solution to this scenario all day now and this is all I have:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /prompt-v3/
#RewriteCond %{REQUEST_URI} ^/prompt-v3/(.*)$
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [R]
The commented-out lines deal with a sub-dir when on a remote host. So far I can redirect to the /public sub-dir if the file exists there and that's about it.
Thank you everyone for your help!
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /sub-dir/
#RewriteCond %{REQUEST_URI} ^/sub-dir/(.*)$
RewriteRule ^index.html$ $1 [L,R=301]
RewriteCond $1 !^public
RewriteCond $1 !^lib
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond $1 ^public/$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ lib/bootstrap.php [L]
RewriteCond $1 !^public/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^public/(.*)$
RewriteRule ^(.*)$ lib/bootstrap.php?path=%1 [L]
This will look for an index.html file in the public directory and if it does not exist rewrite the URL to lib/bootstrap.php. it in fact checks for any request as a static file in the public directory first and deals with canonicalisation too.
Put this .htaccess in your document root
<ifModule mod_rewrite.c>
RewriteEngine on
# For /js/foobar.js (/js/*)
RewriteRule ^js/(.*)$ /public/js/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)$ index.php?controller=$1 [NC,L]
# For foo/bar (/*/*?*)
RewriteRule ^(.*)/(.*)$ /public/$1/$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)/(.*)$ index.php?controller=$1&action=$2&querystring=%{QUERY_STRING} [NC,L]
</IfModule>

.htaccess to nginx rewrite rules excluding certain folders

So im trying to move an application from apache to NginX but I've discovered i need to re write the .htaccess file, I've had a good look around but finally I need to ask for some help.
Below is the .htaccess file from apache, im trying to redirect all requests to index.php except for requests to /html, /css, /images, /php.
Thanks for your help in advance!
Bill
RewriteEngine on
RewriteRule ^html [L,NC]
RewriteRule ^css [L,NC]
RewriteRule ^images [L,NC]
RewriteRule ^php [L,NC]
RewriteRule ^.*$ index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !directory/(.*)$
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
So far I have the below and the requests for the html, css, images and php files are working great.
But when i make a request for www.domain.com/blah/blah/blah/ im getting a 404, what i really want is for the URL to reamin as www.domain.com/blah/blah/blah/ but load the index.php file
location ~ directory/(.*)$ {
}
location ~ (.*)$ {
}
location /html {
rewrite ^/html / break;
}
location /css {
rewrite ^/css / break;
}
location /images {
rewrite ^/images / break;
}
location /php {
rewrite ^/php / break;
}
location / {
rewrite ^(.*)$ /index.php break;
if (!-e $request_filename){
rewrite ^(.*)$ http://www.domain.com/$1 redirect;
}
}
Try something along these lines for filtering requests to content in specific folders:
RewriteEngine On
# if not requesting content in one of the specified folders
RewriteCond %{REQUEST_FILENAME} !^(html|css|images|php)/ [NC]
# redirect visitor to the index page
RewriteRule ^(.*)$ index.php [L,QSA]
The exclamation mark ( ! ) before the rewrite condition means a negation, so if the requested filename DOES NOT start with html or css or ...., apply the rewrite rule.

htaccess rewrite rule, old URL to new

A bit of help fellow SO people.
What I have at the moment (based on some code I used for a different type of URL).
I want the first URL to redirect to the second, with no query string included afterwards
This is what I have to so far.
RewriteRule ^(page.php?id=missionstatement+)/?$ http://example.com/why/mission-statement [R=301,L]
RewriteRule ^(page.php?id=ofsted+)/?$ http://example.com/how/ofsted-report [R=301,L]
RewriteRule ^(page.php?id=governingbody+)/?$ http://example.com/governors [R=301,L]
Here is the rule (will redirect 1 URL):
RewriteCond %{QUERY_STRING} ^id=whatever
RewriteRule ^page\.php$ http://%{HTTP_HOST}/how/somehow? [R=301,L]
This rule intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
I have used %{HTTP_HOST} -- this will redirect to the same domain as requested URL. If domain name has to be different, replace it by exact domain name.
The ? at the end of new URL will get rid of existing query string.
Ahoy!
Give this a whirl:
#check mod_rewrite is enabled
<IfModule mod_rewrite.c>
#enable mod rewrite
RewriteEngine On
#set working directory
RewriteBase /
#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
#bootstrap index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page.php\?id=(.*)$ http://www.willans.com/page.php/$1 [R=310,L]
#end mod rewrite check
</IfModule>
It's been a while since i've done any web dev, but that should be a push in the right direction at least ;)

.htaccess ModRewrite question

trying to do rewrite rule for this:
http://website.com/checkreg/34324234 <--- Old URL Location
to this:
http://website.com/chkreg.php?checkreg=34324234 <--- New URL Address
I've tried the following, but it causes an error 500 message, I don't know enough about ModRewrite to figure out the issue.
# BEGIN WordPress
&#60IfModule mod_rewrite.c&#62
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^checkreg/([^/]*)$ /chkreg.php?checkreg=$1 [L]
RewriteRule . /index.php [L]
&#60/IfModule&#62
# END WordPress
You said you want to rewrite from http://website.com/chkreg.php?checkreg=34324234
to http://website.com/checkreg/34324234, but your rewrite rule is doing the opposite.
Use this:
RewriteRule ^chkreg.php?checkreg=(.*) /checkreg/$1 [L]
Edit: If you need further debugging information from mod_rewrite then add the following:
RewriteLog /your/path/rewrite.log
RewriteLogLevel 3

Resources