htaccess to nginx rewrite issue - apache

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.

Related

.htaccess Rewrite Conditions to nginx config - multiple conditions & rewrite

I have the following rewrites in .htaccess:
#exception for shorthand phpthumb links eg /userfiles/myimage.jpg?w=300
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|png|gif|bmp|JPG|)$
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.*)$ application/third_party/phpThumb/phpThumb.php?src=../../../$1&%1 [NE,L]
It checks that request is for one of the specified (image) files and that has some query string arguments. In that case it should rewrite to an automatic thumbnail script.
I am trying to rewrite that into nginx config. So far I've got this:
location \.(jpg|jpeg|png|gif|bmp|JPG)$ {
if($args ~ "^(.+)$"){
rewrite ^/(.*)$ /application/third_party/phpThumb/phpThumb.php?src=../../../$1 last;
}
}
I am new to nginx and tried to glue together the new config from some manuals and this converter:
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/
But it doesn't seem to work. What am I doing wrong?
I have replaced the rewrite with the following:
location ~ /(images|userfiles)/ {
if ($args) {
# alternative shorthand phpthumb links eg /userfiles/myimage.jpg?w=300
rewrite .(jpg|jpeg|png|gif)$ /application/third_party/phpThumb/phpThumb.php?src=../../..$uri last;
}
}

.htaccess url rewriting is working but not the redirection

I'm working on a local server on an html/php application and I'm trying to use the Apache url rewrite module without success
The application was stored in ./Compta/index.php. I have to an .htaccess file in ./Compta/.htaccess
I would like to only use a rewritten url like :http://localhost/Compta/Test/
instead of : http://localhost/Compta/index.php?page=test
and redirect users if they try to go to the old url
The .htaccess file contains :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L]
RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301]
When I go to http://localhost/Compta/Test/ the following line is working and my code includes in a div the content of test.php :
RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L]
When I go to http://localhost/Compta/Test the following line is working but in firefox the url is rewritten to http://localhost/Compta/index.php?page=Test and this is not happening with http://localhost/Compta/Test2; the url isn't rewritten.
RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L]
To fix this and to redirect the old url I added these lines :
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301]
But this is not working and when I go to http://localhost/Compta/index.php?page=Test the url isn't rewritten to http://localhost/Compta/Test/
Thank you in advance
I didn't find a solution with .htaccess but i found one with php so i added the following lines in top of my php files :
if(preg_match("/\/Compta\/index\.php\?page=([[:alnum:]]*)/",#$_SERVER['REQUEST_URI'],$page)&&!empty($page[1]))
{
$new_url = "/Compta/";
switch (strtolower($page[1])) {
case "test":
$new_url = $new_url."Test/";
break;
case "test2":
$new_url = $new_url."Test2/";
break;
default:break;
}
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$new_url);
exit;
}
I test if the url begin with "/Compta/index.php?page=" AND if there is a parameter for "page"
Then I initialise the variable which contain the new url
Switch the content of my parameter I modify the new url
and then I make the redirection to my new url :)

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".

Htaccess rule to nginx

i'm trying to rewrite the htaccess rule to nginx rule.
this rule is used to load a php page for robot (like google robot).
here my htaccess:
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteCond %{REQUEST_URI} ^/faq/$
RewriteCond %{QUERY_STRING} ^(.*)_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ /files/snapshot_loader.php?snapshot_page=%1%2 [L]
can you help me ? i try this one, but redirect never work....
location /faq {
if ($query_string ~ "^(.*)test(.*)$"){
RewriteRule ^(.*)$ http://www.google.com permanent
}
}
Thanks in advance
First, there is no $query_string variable in nginx. And the second thing is there is no RewriteRule directive in nginx.
In nginx, the query string is stored in $args and you want the rewrite directive:
Try something like:
location /faq {
if ($http_host ~* "^localhost$") {
if($args ~* "^(.*)_escaped_fragment_=(.*)$") {
set $newargs $1$2
set $args '';
rewrite ^ /files/snapshot_loader.php?snapshot_page=$newargs
}
}
}

'nginx rewrite' to '.htacces rewrite'

Im very bad in regexps, but in server-configs im the worst.
So any help ll be usefull.
have such in nginx-config
location / {
rewrite ^(.*[^/])$ $1/ permanent;
try_files $uri $uri/ /index.php?$args;
}
need EQUAL thing for .htacces.
Ty.
They should prove very similar.
<IfModule mod_rewrite.c>
RewriteEngine On
# Your web directory root
RewriteBase /
#Make sure it's not a specific file or directory that they're trying to reach
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*[^/])$ index.php?args=$1 [NC, L]
</IfModule>
This will:
Mark RewriteEngine on for the folder the .htaccess is in (if you've got the module enabled);
Specify conditions to avoid redirection if the file is a directory or a file on the server; And
Redirect your Regex to index.php?args={everything caught by the regex}
The [NC, L] flags tell the rule to be not case sensitive and tell Apache this is the last redirection rule, respectively.
I have to admit, I'm not sure what your Regex is testing for though. Would you mind explaining the URL scheme you want to put in place? I should be able to help you formulate the Regex properly.