简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM