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;
}
}
Related
i'm trying to add alongside an old Wordpress website a route that servers a NodeJS Instance.
The thing is that I want to run both Wordpress and Node.js on the same server but on different routes.
Wordpress is running here http://example.com
Node is running here http://example.com:61000/oferta-de-pret-traduceri
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/oferta-de-pret-traduceri$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/oferta-de-pret-traduceri/(.*)$ http://127.0.0.1:61000/$1 [P,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I don't know Wordpress that well nor do I know .htaccess like I should in this case and I don't want to move everything to NGINX unless I have to.
I want my Node.js instance to run on http://example.com/oferta-de-pret-traducerii not on http://example.com:61000.
Also Wordpress trows a 404 error if I try to access http://example.com/oferta-de-pret-traduceri
Hope someone has encountered this kind of situation and has an easy solution.
Thank you very much in advance !
You might be better off with ProxyPassMatch https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassmatch
ProxyPassMatch "^/oferta-de-pret-traduceri$" "http://127.0.0.1:61000/$1"
The docs for mod_rewrite say this:
Consider using either ProxyPass or ProxyPassMatch whenever possible in
preference to mod_rewrite.
If you cannot use ProxyPass, try this rule:
RewriteRule ^oferta-de-pret-traduceri http://127.0.0.1:61000$1 [P,L]
I used this tool to test: http://htaccess.mwl.be/
If your node app expects 'oferta-de-pret-traduceri' to be in the URL, you will need this rule:
RewriteRule ^oferta-de-pret-traduceri http://127.0.0.1:61000/oferta-de-pret-traduceri$1 [P,L]
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
Using symfony for my website and i have a 302 redirection from my https://www.exemple.com/ to https://www.exemple.com/, and google doesn't want 302 redirection... It is the last condition of the rewrite section of the .htaccess which does that.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
I tried to put instead :
RewriteRule .? %{ENV:BASE}/app.php [R=301,L]
But in that case i have a 301 redirection but my site is not reachable. Any ideas ?
Find solution,
It was not the .htaccess in cause. I'm new with 301/302 redirection and didn't know u can spécifiate it in controller. So i add a ",301" at the end of redirections i do in controller like that :
return $this->redirect($this->generateUrl('sdzbikind_acceuil'), 301);
Hope it helps somebody else... I'm just asking now if i should do 301 redirection in all my symfony project redirections !?
I have the following rewrite rule:
RewriteRule ^(.*)/white/(.*)/$ /$1/$2/?tags=white [R=301,L]
It works and changes the following:
http://bag-saver.com/uk/shop/white/clutch-bags/
into:
http://bag-saver.com/uk/shop/clutch-bags/?tags=white
But, I don't want the 301. I want to simply rewrite the URL. I've tried the following:
RewriteRule ^(.*)/white/(.*)/$ /$1/$2/?tags=white [QSA,L]
But as soon as I remove the R=301, I get page not found errors.
Why is this?
Edit - here are the rest of the rules in place:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)-[0-9]{9}[0-9]+/$ /$1/ [R=301,L]
RewriteRule ^(.*)/white/(.*)/$ /$1/$2/?tags=white [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
use the [P] flag
RewriteRule ^(.*)/white/(.*)/$ /$1/$2/?tags=white [P]
update
other option is just remove the R=301 to avoid forced redirect
RewriteRule ^(.*)/white/(.*)/$ /$1/$2/?tags=white [L]
To fix page not found error, you could try to set index.php as default page in Apache, if that is missing, to do that open the apache configuration file httpd.conf :
vi httpd.conf
Find out line that read as follows:
DirectoryIndex
Now set it as per your requirements:
DirectoryIndex index.html index.htm index.php
Save and close the file. Restart the Apache web server:
/etc/init.d/httpd restart
Please note that if you do not have access to Apache configuration file. Add DirectoryIndex to your .htaccess file.
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.