I have 2 websites that can be both accessed at the same URL, only one is accessible from a subfolder like this:
website.com/
website.com/blog
Now they are running under Apache and I want to switch to Nginx and I stumbled upon the rewriting rules of Nginx in this approach.
The hosting server is an Ubuntu with Plesk so the only part I need to edit is the one that is responsible for the rewrites based on the URL format.
The original starting point is this one:
if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
set $test "${test}C";
}
if ($test = PC ) {
rewrite ^/(.*)$ /index.php?$1;
}
This works great for the main website, however, the one located in blog will show the homepage but any other inner page will return a 404 error.
So the first approach I had towards this was this one:
if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
set $test "${test}C";
}
if ($uri ~ ^/(*blog*)) {
set $test "B";
}
if ($test = PC ) {
rewrite ^/(.*)$ /index.php?$1;
}
if ($test = B ) {
rewrite ^/(.*)$ /blog/index.php?$1;
}
But this does not work so I tried an approach based on location like this:
location / {
if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
set $test "${test}C";
}
if ($test = PC ) {
rewrite ^/(.*)$ /index.php?$1;
}
}
location ~ /blog {
root httpdocs/blog
if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
set $test "${test}C";
}
if ($test = PC ) {
rewrite ^/(.*)$ /index.php?$1;
}
}
It works with the current configuration:
if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location|blog)) {
set $test "${test}C";
}
if ($uri ~ ^/(blog)) {
set $test "${test}B";
}
if ($test = PC ) {
rewrite ^/(.*)$ /index.php?$1;
}
if ($test = PB ) {
rewrite ^/(.*)$ /blog/index.php?$1;
}