NGINX to Apache(rewriting) - apache

Could someone please try converting this into .htaccess for me? I am not good at apache, and my new server setup needs apache:
listen 443;
ssl on;
ssl_certificate /etc/nginx/conf.d/pgcp.pingrglobe.crt;
ssl_certificate_key /etc/nginx/conf.d/pgcp.pingrglobe.key;
server_name pgcp.pingrglobe.com;
root /var/www/html/pingrglobe.com/ctrlpanel;
index index.html index.php;
error_page 404 https://www.pingrglobe.com/404.html;
location / {
try_files $uri $uri/ #extensionless-php;
add_header Access-Control-Allow-Origin *;
}
rewrite ^/blog/blogpost/(.+)$ /blog/blogpost?post=$1 last;
rewrite ^/viewticket/(.+)/(.*)$ /viewticket?tid=$1&$2 last;
rewrite ^/vemail/(.+)$ /vemail?eid=$1 last;
rewrite ^/serversettings/(.+)$ /serversettings?srvid=$1 last;
rewrite ^/notification/(.+)$ /notification?id=$1 last;
rewrite ^/viewreport/(.+)$ /viewreport?srvid=$1 last;
rewrite ^/removeserver/(.+)$ /removeserver?srvid=$1 last;
rewrite ^/staffviewticket/(.+)/(.*)$ /staffviewticket?tid=$1&$2 last;
rewrite ^/activate/(.*)/(.*)/(.*)$ /activate?user=$1&code=$2&email=$3 last;
rewrite ^/activate2/(.*)/(.*)/(.*)$ /activate2?user=$1&code=$2&email=$3 last;
rewrite ^/passwordtoken/(.+)/(.*)/(.*)$ /passwordtoken?user=$1&token=$2&email=$3 last;
Thank you!

A lot of that stuff needs to go into your server/vhost's config, like the ssl stuff, the document root stuff, the server name, etc. You'll need to read the apache documentation for that. The rewrite stuff, however, can go in the htaccess file:
ErrorDocument 404 https://www.pingrglobe.com/404.html
DirectoryIndex index.html index.php
RewriteEngine On
RewriteRule ^blog/blogpost/(.+)$ /blog/blogpost?post=$1 [L]
RewriteRule ^viewticket/(.+)/(.*)$ /viewticket?tid=$1&$2 [L]
RewriteRule ^vemail/(.+)$ /vemail?eid=$1 [L]
RewriteRule ^serversettings/(.+)$ /serversettings?srvid=$1 [L]
RewriteRule ^notification/(.+)$ /notification?id=$1 [L]
RewriteRule ^viewreport/(.+)$ /viewreport?srvid=$1 [L]
RewriteRule ^removeserver/(.+)$ /removeserver?srvid=$1 [L]
RewriteRule ^staffviewticket/(.+)/(.*)$ /staffviewticket?tid=$1&$2 [L]
RewriteRule ^activate/(.*)/(.*)/(.*)$ /activate?user=$1&code=$2&email=$3 [L]
RewriteRule ^activate2/(.*)/(.*)/(.*)$ /activate2?user=$1&code=$2&email=$3 [L]
RewriteRule ^passwordtoken/(.+)/(.*)/(.*)$ /passwordtoken?user=$1&token=$2&email=$3 [L]
In general, you want to remove the leading slash from the match, and you want to replace the last; with [L], and that's it.

Related

Nginx Rewrite Multiple Rule

I am trying to move my server to Nginx for trial purposes. However, when I transfer my htaccess codes to conf file, I get 404 error. How can I make the codes below Nginx rewrite compatible?
RewriteEngine on
RewriteRule ^firmaekle\.html$ /firmaekle.php [L,QSA,R=301]
RewriteRule ^/d/([^/]*).([^/]*)\.html$ /index.php?site=$1&site2=$2 [L,QSA,R=301]
RewriteRule ^u/([^/]*)([^/]*)\.html$ /uzgun.php?site=$1&site2=$2 [L,QSA,R=301]
RewriteRule ^y/([^/]*)([^/]*)\.html$ /yonlendir.php?firma=$1&uzanti=$2 [L,QSA,R=301]
RewriteRule ^ye/([^/]*)([^/]*)\.html$ /yonlendir2.php?firma=$1&uzanti=$2 [L,QSA,R=301]
you can do it like this
server {
server_name example.com;
rewrite ^/firmaekle\.html$ /firmaekle.php permanent;
rewrite ^//d/([^/]*).([^/]*)\.html$ /index.php?site=$1&site2=$2 permanent;
rewrite ^/u/([^/]*)([^/]*)\.html$ /uzgun.php?site=$1&site2=$2 permanent;
rewrite ^/y/([^/]*)([^/]*)\.html$ /yonlendir.php?firma=$1&uzanti=$2 permanent;
rewrite ^/ye/([^/]*)([^/]*)\.html$ /yonlendir2.php?firma=$1&uzanti=$2 permanent;
}

htaccess to nginx rewrite issue

In my code I am trying to convert Apache .htaccess into Nginx using online convertor. I even edited this file etc/nginx/conf.d/domainname.conf but the rewrite URLS won't work. I have two htaccess files, one of which used in root folder and the second one used in administration folder of my script.
Here is the root's htaccess file content
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?slug=$1
htaccess converted code for Nginx
location / {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
Here is the administration folder htaccess file content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^(.*)/$ /$1.php [L,R=301]
Administration folder htaccess converted code for Nginx
location /administration {
if (!-e $request_filename){
rewrite ^(.*)$ /$0.php break;
}
rewrite ^/(.*)/$ /$1.php redirect;
}
I pasted the converted content into domainname.conf (domainname is my domain) and restart the ngnix but it won't work at all. I don't know whether my converted code is accurate or not or any thing else I am missing through it.
Simply edit the file i.e. etc/nginx/conf.d/yourdomain.tld.conf and look for the following code like this:
#location / {
#Uncomment 3 lines and set your rules here!
#}
You need to un-comment these 3 lines of code or paste this code instead:
location / {
if (!-e $request_filename) {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
}
location /administration/ {
if (!-e $request_filename){
rewrite ^(.*)$ $1.php last;
}
}
Then you must require to restart Nginx, if restarted without any errors it will work accurately otherwise Nginx will stop working, this step is important.

How to convert rewrites from advanced .htaccess to nginx conf rules

Recently switched to nginx from Apache. This works under apache perfectly fine, but don't know how to add it for nginx. Have tried htaccess to nginx converters, but they get me redirect loop.
I have WordPress in root and custom code under subdirectory.
This is the working .htaccess file on Apache:
# rewrite engine on and setting up base
RewriteEngine On
RewriteBase /leffa/
# replace + with _
RewriteRule ^(.+?)\+(.+)$ $1-$2 [R=301,L,NE]
# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /index(?:\.php)?\?q=([^\s&]+)&(kuvauksesta)= [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /index(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ %1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual URL (extra parameter)
RewriteRule ^([^/.]+)/([^/.]+)?$ index.php?q=$1&$2=1 [L,QSA]
# internal forward from pretty URL to actual URL
RewriteRule ^(.+)/?$ index.php?q=$1 [L,QSA]
This rewrites all the urls like http://www.rollemaa.org/leffa/index.php?q=the+boy to pretty ones like http://www.rollemaa.org/leffa/the-boy.
The situation is, I have main file set up like this:
server {
listen 80;
access_log /var/log/nginx/rollemaa.access.log;
error_log /var/log/nginx/rollemaa.error.log;
root /var/www/rollemaa.org/public_html;
index index.html index.htm index.php;
server_name rollemaa.org www.rollemaa.org;
include hhvm.conf;
include global/wordpress.conf;
# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
# Static File Caching
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
#location /leffa/ {
#try_files $uri $uri/ /leffa/index.php?q=$args;
#rewrite ^/leffa/(.+?)\+(.+)$ /leffa/$1-$2 redirect;
#rewrite ^/leffa/(.*)$ /leffa/%1/%2? redirect;
#rewrite ^/leffa/(.*)$ /leffa/%1? redirect;
#if (-e $request_filename){
# rewrite ^/leffa/([^/.]+)/([^/.]+)?$ /leffa/index.php?q=$1&$2=1 break;
#}
#rewrite ^/leffa/(.+)/?$ /leffa/index.php?q=$1 break;
#}
}
As you can see, I have commented out the rewrite part, because it's not working.
Any nginx gurus out there who could help me with this? Much appreciated in advance!
You can use the following rewrite rule.
location /leffa/ {
index index.php index.html;
rewrite ^/leffa/([^/]*)/?$ /leffa/index.php?q=$1;
}
It will rewrite URLs like /leffa/the-boy/ and /leffa/the-boy to /leffa/index.php?q=the-boy. URLs with sub-subdirectories such as /leffa/the-boy/something-here will be ignored.
Note: + in the URL is not converted to a space (as it would be when directly accessing /leffa/index.php?q=the+boy). Accessing /leffa/the+boy/ will result in the query parameter q being "the+boy".
If you want to use spaces in the query string, you will have to use the %20 URL encoded format for spaces. /leffa/the%20boy and /leffa/the%20boy/ will result in the query parameter q being "the boy".

Convert .htaccess to nginx, seeking working solution

Hello i am aware that there are some .htaccess2nginx converter outside, and well, still need some help, since it's not working at all.
To my problem:
Setting up an Imageboard(MyImouto) on Nginx,
Imageboard uses .htaccess to redirect from /srv/domain/public/ to /srv/domain/app/...
Here is the .htaccess:
RewriteEngine On
# Images redirection.
# The following RewriteCond will try to deny any direct linking to images.
# RewriteCond %{HTTP:Referer} ^http://domain/
RewriteRule ^(?:data/)?(preview|sample|jpeg|image)/([0-9a-f]{2})([0-9a-f]{2})([$
# Serve gzipped Stylesheets and Javascripts if mod_headers are enabled.
<IfModule mod_headers.c>
SetEnv no-gzip
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(assets/.*)\.css $1\.css\.gz [L,ENV=ASSETSCSS:true] [L]
Header set Content-Type text/css env=ASSETSCSS
Header set Content-Encoding gzip env=ASSETSCSS
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(assets/.*)\.js $1\.js\.gz [L,ENV=ASSETSJS:true] [L]
Header set Content-Type text/javascript env=ASSETSJS
Header set Content-Encoding gzip env=ASSETSJS
</IfModule>
# Redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]
Converting with one of the 3 converters i used didn't even worked.
The other both throw out something like this:
rewrite ^/(?:data/)?(preview|sample|jpeg|image)/([0-9a-f]{2})([0-9a-f]{2})([$ /;
if ( ~ ""){
set $rule_1 1$rule_1;
}
if ($rule_1 = "1"){
rewrite / /;
}
if ( ~ ""){
set $rule_2 1$rule_2;
}
if ($rule_2 = "1"){
rewrite / /;
}
if (!-f $request_filename){
set $rule_3 1$rule_3;
}
if ($rule_3 = "1"){
rewrite /.* /index.php last;
}
But it's still not working.
Would be nice if you could help me for an solution :)
First of all, you have to understand what this .htaccess do.
Except of incomplete rewrite this .htaccess tries to serve precompressed assets (file.css.gz instead of file.css) and rewrites requests for non-existent files to index.php.
I would write something like this:
location / {
try_files $uri $uri/ /index.php;
}
location ^~ /assets/ {
gzip_static on;
}
location ~ \.php$ {
try_files $uri /index.php;
# other PHP fastcgi stuff
}
Reference:
gzip_static
try_files

Apache mod_rewrite to Nginx rewrite rules

My site is running on Nginx and I am trying to add a software in the sub-directory of the site that uses Apache's mod_rewrite rules. E.g. www.mydomain.com/mySubfolder
Here is the Apache .htaccess
#Options -Indexes
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
</ifModule>
So far I managed to get the main page to work but when requesting the login page, it is causing a URL redirect loop. E.g. www.myDomain.com/login
With this:
location /mySubfolder {
if (!-e $request_filename) {
rewrite ^(.*)$ /mySubfolder/index.php?q=$1 last;
break;
}
}
I have been reading and trying to learn how to convert Apache to Nginx and even used the .htaccess to Nginx converter I found at http://winginx.com/htaccess but the tool doesn't seem to recognize the %{REQUEST_URI} ^/system.* part. Upon my research and study, I came up with:
location /mySubfolder {
if ($request_uri ~ "^/(system.*)$") {
rewrite ^/(.*)$ index.php?/$1 last;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /mySubfolder/index.php?q=$1 last;
break;
}
}
I am a complete noob at this and was even wondering if I am even close to accomplish this conversion to work. Please help.
Thank you.
was even wondering if I am even close to accomplish this conversion to
work
You've basically taken the wrong approach to use in Nginx.
Although it is kind of natural to assume that the rewrite rules in Apache would be mapped to the rewrite in Nginx, they aren't. Instead they are mostly mapped to the location rules.
Your block for mySubfolder should look like:
location ^/mySubfolder {
try_files /mySubfolder/index.php?$args =404;
}
You aren't actually rewriting anything - you are just telling nginx that any requests that start with /MySubfolder should be served by the files listed in try_files. btw you have to tell Nginx to pass the query string through which is what the args is doing.
You can append the original URL (i think) though it may be easier just to use $_SERVER['REQUEST_URI'] inside your script.
I believe the rewrite rule you have to the same start URI is causing the /mySubFolder to keep matching.
In Nginx rewriting is only used when you want to make external URL paths be served by different internal urls, and normally the rewrite rule is not inside a location block.
For example I have a file server, that serves up images and other files. I have these rewrite rules in the server block:
rewrite ^/image/(\d+)/(\w+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&mode=$2&imagePath=$3.$4&resizeAllowed=TRUE&type=image last;
rewrite ^/image/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyImage.php?typeID=$1&imagePath=$2.$3&resizeAllowed=TRUE last;
rewrite ^/file/(\d+)/(.+)\.([^\.]*)$ /proxy/proxyFile.php?typeID=$1&imagePath=$2.$3&resizeAllowed=FALSE last;
to make the external URLs look nice. But then they are all served by the location block:
location ~* ^/proxy {
try_files $uri =404;
fastcgi_pass unix:/opt/local/var/run/php54/php-fpm-images.sock;
include /documents/projects/intahwebz/intahwebz/conf/fastcgi.conf;
}
The location block doesn't need to know about the URL rewrites because they are done before the location block is encountered.