Lumen in a subfolder trailing slashes issue - nginx

In public/index.php I have changed $app->run() to $app->run($app['request']) and this resulted in Lumen working in a sub-folder, so this works:
http://local.dev/app/
http://local.dev/app/test
However if there's a slash at the end of a route then I get a NotFoundHttpException. For example:
http://local.dev/app/test/
I'm using NGINX and my rewrite rule for the folder is:
location /app/ {
try_files $uri $uri/ /app/index.php?$query_string;
}
Am I doing something wrong?

If laravel is in a sub-directory use this :
RewriteRule ^(.*)/$/ /$1 [L,R=301]
instead of this:
RewriteRule ^(.*)/$ /$1 [L,R=301]

Related

How do I properly add try_url or rewrite for a .html add?

So, coming from the Apache world I am new to configuring NGNIX more than just the root site. I have tried to use an Apache to NGNIX rewrite, but the output doesn't seem to render the pages correctly.
Original .htaccess code:
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
The output doesn't seem to work after reloading the new configuration.
I have been trying different configurations under my location block trying to just add .html and end of the URI/URL. This is for a sub-directory called "wiki" just serving static HTML files.
Here's my current configuration under HTTPS:
root /var/www/html/;
index index.php index.html;
...
location ^~ /wiki/ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^(.+)/$ $1.html permanent;
}
The alias or path is /var/www/html/wiki
Please advise. I was able to get /wiki/index.html to render, but any other HTML files do not rewrite from wiki/product to product/product.html. I keep getting 404 errors.

Laravel with Wordpress blog (with WP API)

My progect structure
/root_dir
/app
/bootstrap
...
/public
index.php
.htaccess
/wp #wordpress installation here
/wp-admin
/wp-content
/wp-includes
index.php
.htaccess
...
So what I want to acomplish is to have Laravel project with views, API etc
And I need wordpress only for one route domain.com/blog where I'll have separate default WP blog that doesn't integrate or interact with my Laravel app in any way
My /public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^(blog) #ADDED ONLY THIS MAGIC LINE
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And my /public/blog/.htaccess is WP default
And it works like a charm!
My lara routes are fine... API routes - good... WP blog - great...
BUT
Now I want to call WP API, e.g. domain.com/blog/wp-json/wp/v2/posts
And all I see is Laravel's "NOT FOUND" page
Please help!
I have a strong feeling it can be solved with .htacess 'magic' but unfortunately I'm noob in that...
P.S. Found a similar question here Laravel project next to Wordpress project (in public_html folder)
But I need vice versa solution
you can proxy pass the address http://example.com/blog to some other address,
for example using nginx :
server {
listen 80;
server_name blog.example.com;
location / {
root /var/www/wordpress;
try_files $uri /index.html;
}
}
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/laravel;
try_files $uri /index.html;
}
location /blog/ {
proxy_pass http://blog.example.com/;
}
}
This is a simple and also clean way, and also you don't need to change even one line of code to have laravel and wordpress aside each other.
Holy cow!
The solution was to navigate to dashboard/settings/permalinks and change option from 'plain' to any other!
Found the answer here https://wordpress.org/support/topic/rest-api-in-wp-4-7/#post-8620246
You need to be using pretty permalinks to access /wp-json/. If you’re not, you can instead use ?rest_route=/ to get the index and ?rest_route=/wp/v2/posts (e.g.) for specific routes.

Nginix Routing For Wordpress

I'm trying to get Nginx and Wordpress to play nice together.
Most of the documentation I've found talks about how to set up Nginx rewrite rules for WP Multisite configurations, but I'm trying to do something slightly different, where I'm looking to load data from an API and populate a template wordpress page, but have the url as the last path component.
For example I'm trying to load a page with a URL like this…
http://example.com/article/{the-slug}
When loading this page we want to redirect to a Wordpress page located at a permalink of this
http://example.com/article
We do no want to load any post content from the existing word press site,
We simply always go to this page when any url matching /article/{slug} is matched, and pass off the slug parameter behind the scene.
We have a WP Rewrite rule in place in our theme’s functions.php
/**
* Intercept the articles request
* #return void
*/
function custom_rewrite_rule() {
add_rewrite_rule('^article/([^/]*)/?','index.php/article?slug=match[1]','top');
}
add_action('init', 'custom_rewrite_rule’);
When running in Apache everything works exactly as expected when the following rules specified in the .htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^^article/([^/]*)/? /index.php/article?slug=match[1] [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
However I’ve tried to convert this to run on Nginx with no success.
Some variations I’ve used for the location directive are
location / {
rewrite ^/^article/([^/]*) /index.php/article/?slug=match[1];
try_files $uri $uri/ /index.php$query_string;
}
And
location / {
try_files $uri $uri/ ^article/([^/]*)/? /index.php/article?slug=match[1];
try_files $uri $uri/ /index.php$query_string;
}
And even a combo of these together.
location /article {
try_files $uri $uri/ ^article/([^/]*)/? /index.php/article?slug=match[1];
}
location / {
try_files $uri $uri/ /index.php$query_string;
}
But I’m really stumped on the actual syntax to get this to work
Any advice would be greatly appreciated.
Thanks.
Update
I've been logging rewrites in Nginx and things there look like they're doing exactly what they should
2016/06/16 02:56:52 [notice] 12879#12879: *1 "^/article/(.+)" matches "/article/test-article", client: 192.168.10.1, server: example.com, request: "GET /article/test-article HTTP/1.1", host: "example.com"
2016/06/16 02:56:52 [notice] 12879#12879: *1 rewritten data: "/index.php/article", args: "", client: 192.168.10.1, server: example.com, request: "GET /article/test-article HTTP/1.1", host: "example.com"
And if I place this url in the browser http://example.com/index.php/article/, I go exactly where i want the rewrite to take me.

Rewrite rule in nginx error

What is wrong in this rule?
rewrite ^/view/?([^/.]+)?/?([^/.]+)?.html /view_video.php?id=$1 last;
I changed from:
RewriteRule ^view/?([^/\.]+)?/?([^/\.]+)?\.html view_video.php?id=$1 [L,QSA]
My link must be:
./view/14/name_of_video.html

Pinnect's htaccess rules to nginx

so I've been playing around with the rules trying to transform them, the pinnect script's rules are:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
#RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# fix for uploadify 302, 406 errors
SetEnvIfNoCase Content-Type "^multipart/form-data;" "MODSEC_NOPOSTBUFFERING=Do not buffer file uploads"
I have pinnect on pinnect/ on the server, however VB is running on the index for now,
I've tried
location /pinnect {
try_files $uri $uri/ /index.php;
}
And different variations of that, however let's say I go to the /install directory - it tries to redirect me to pinnect/forums.php(because of VB), if I try to go to index.php directly, an endless loop occurs, anyone could give me a hand with this?
EDIT:
So i've got this rule currently
location ~ ^/pinnect/index.php { try_files maintenance.html #index.php; }
location ~ ^/pinnect/ {
rewrite ^/pinnect$ /pinnect/index.php last;
rewrite ^/pinnect/$ /pinnect/index.php last;
#rewrite ^/pinnect/([^.]+)$ /pinnect/index.php/$1 last;
rewrite ^/pinnect/(.*) /pinnect/index.php/$1 last;
}
However where ever I go, I get a 301, 302 so it's not behaving same way as the htaccess, could anyone help?
If it's just a matter of status code returned, you might have to use return instruction on your location block.
location ~ ^/pinnect/index.php { try_files maintenance.html #index.php; }
location ~ ^/pinnect/ {
rewrite ^/pinnect$ /pinnect/index.php last;
rewrite ^/pinnect/$ /pinnect/index.php last;
#rewrite ^/pinnect/([^.]+)$ /pinnect/index.php/$1 last;
rewrite ^/pinnect/(.*) /pinnect/index.php/$1 last;
return 200;
}
Read more…
As we are here, you should replace last by break since you have your instructions in a location block.
Also, you might prefer using ? instead of (.*) to drop the original arguments.
Read more…

Resources