I'm trying to set up nginx vhost so I can use wordpress and laravel on the same domain.
I put wordpress in the root folder
And in a subfolder /laravel/ installed laravel
Is it possible to make that if for example laravel route /laravel-page in URL, request on server will not go to wordpress /index.php but to Laravel /laravel/public/index.php,
and all other pages are normally requested to /index.php
I would like to whitelist URLs using RegExp, which will be handled by Laravel.
UPDATE:
After a few experiments, I managed to get what I wanted. I added:
location ~ ^/laravel-page(?:/(.*))?$ {
index /laravel/public/index.php;
}
These pages are now handled by Laravel: /laravel-page/1333/. But for some reason, if I enter the address without the slash /laravel-page/1333 , it gives a 404 error, and not from wordpress or laravel, but the usual one from nginx. I tried adding a redirect:
rewrite ^/(.*)/$ /$1 permanent;
It works, but laravel doesn't open.
Related
I've have 2 sites, a Laravel/Vuejs application running on my main domain (e.g. website.com) and a simple WordPress blog on a subdomain (e.g. blog.website.com). I don't want to place my WordPress blog as a subfolder in my public folder of my Laravel app, but I want to keep both sites separate like they are now.
I'm trying to figure out how to redirect or show the contents of my WordPress blog on a '/blog' url on my Laravel application. website.com/blog should get the contents of blog.website.com and blog.website.com should redirect to website.com/blog.
Below you can find the redirect in my .htaccess file, rewriting my blog.website.com to website.com/blog, which works like a charm! My subdomain redirects to my Laravel application with a trailing '/blog'.
//
// .htaccess
//
# Rewrite subdomain to main url.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.website.com [NC]
RewriteRule ^(.*)$ https://website.com/blog/$1 [R=301,L]
</IfModule>
Now this is where the tricky part starts. My Laravel application displays a Vuejs (with Vue Router) application, with my web.php forcing any route (except 'api' or 'blog') to return the welcome view, containing my Vuejs assets (CSS, JS, etc). I've also added a '/blog' exception to my web.php file, in order to redirect visitors when they visit the blog page as you can see in my code below:
//
// web.php
//
Route::get('/{any?}', function (){
return view('welcome');
})->where('any', '^(?!api|blog).*$');
Route::get('/blog', function () {
return redirect('https://blog.website.com/');
});
Everything bundled up together results as expected in the ERR_TOO_MANY_REDIRECTS error, or an infinite redirect loop. Visiting blog.website.com results into a url rewrite to website.com/blog, which in it's turn redirects 'back' to blog.website.com.
Removing the https://blog.website.com/ redirect from the web.php file results in Laravels' default 404 page not found. So, how could I tackle this redirect issue keeping my Laravel and WordPress environment separate from each other?
EDIT:
As Kamlesh Paul suggested, I've tried editing my nginx.conf file by adding the server block inside of the http block:
//
// /etc/nginx/nginx.conf
//
http {
...
server {
server_name blog.website.com;
return 301 https://www.website.com/blog$request_uri;
}
}
After removing all other possible solutions (.htaccess and web.php changes) and a server/nginx restart nothing happens.
Despite trying several solutions from the comments, I've managed to solve my question by using a symlink!
Logged into my server I've navigated to the public folder of my Laravel application (the below folder structure may vary on different OS).
cd /var/www/vhosts/website.com/httpdocs/public
Using the following code I've created a symlink or symbolic link that acts (more-or-less) as a shortcut to my subdomain.
ln -s /var/www/vhosts/website.com/blog.website.com/ blog
This code creates a blog folder in the public folder of my Laravel application and links it to the root folder of my WordPress blog.
Next, I've removed the redirect to the blog subdomain in my web.php, which now looks like the following:
//
// web.php
//
Route::get('/{any?}', function (){
return view('welcome');
})->where('any', '^(?!api|blog).*$');
Excluding my Laravels' application url with a trailing /blog from serving a specific view, allows the Laravel application to fallback on retrieving the blog folder in the public folder. Since the blog folder is linked to my subdomain, the WordPress blog is shown!
This allowed me to log into my WordPress dashboard in order to change my blogs' site-url and home address in the General settings from https://blog.website.com to https://website.com/blog. This also fixes/implements the new URL structure to all the post types (posts, pages, etc). If this doesn't fix it for you just visit the 'Permalinks' page to automatically flush all URL's (even though it looks like nothing has happend).
The final step was to create a redirect for any direct traffic visiting the old blog.website.com url, which I've added to my .htaccess file in the following way:
//
// .htaccess
//
# Rewrite subdomain to main url.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.website.com [NC]
RewriteRule ^(.*)$ https://website.com/blog/$1 [R=301,L]
</IfModule>
I have a dilemma. I migrated my wordpress site from Apapache server to NGINX.
In the process I changed permalink in WP from
/index.php/%postname%/
to
/%postname%/
Now, users coming to site from Google, are getting 404's because of the permalink change. Typically I would just redirect any page via WP plugin, but because of this index.php in the permalink, plugins don't work. So I have no choice but to create a redirection somewhere in NGINX conf file.
Please advise what to do.
server {
rewrite ^/index.php/(.*)$ /$1 permanent;
...
}
In the server configuration file (the file will be located at /etc/nginx/nginx.conf).
If it does not exist there, it may also be at /usr/local/nginx/conf/nginx.conf or /usr/local/etc/nginx/nginx.conf.
For temporary redirect:
rewrite ^/oldlocation$ http://www.newdomain.com/newlocation redirect;
For permanent redirect:
rewrite ^/oldlocation$ http://www.newdomain.com/newlocation permanent;
I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:
https://example.com/site1/?rest_route=/wp/v2/posts
But this endpoint gives a 404:
https://example.com/site1/wp-json/wp/v2/posts
I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:
location /site1/wp-json {
rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}
location /site1/ {
try_files $uri $uri/ /site1/index.php$is_args$args;
}
I can't see any special handling of wp-json in the WordPress docs or the nginx wiki. What am I missing here? The permalinks for the site is set to Numeric (https://example.com/site1/archives/123) if that might play a role.
Update
Gist of the redacted full config file and the config syntax lints okay:
nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I just hit this too, in WP 5.7. Without pretty permalinks, ie with the "Plain" option like ?p=123, my nginx WP installation uses requests like:
/index.php?rest_route=/wp/v2/users/&who=authors...
And these all work fine.
However if I enable pretty permalinks, eg "Post name", /sample-post/, it starts making requests like:
/wp-json/wp/v2/users/?who=authors...
And these all return a 404. For example, editing or publishing posts fails, and browser devtools shows a string of 404s in this format.
But now we know the pattern that works, a solution is clear - we just need to map the not-working format to the working format:
# Resolves WP Gutenberg 404 issue
location /wp-json {
rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 last;
}
I believe that the rewrite directive should be written as shown below:
server {
location /site1/wp-json
{
rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
}
}
I was able to resolve it like this:
location /wordpress/ {
rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
}
An easy way if your website pages in the subfolder is already working, just add index.php to the url:
https://site1.com/site2/index.php/wp-json/
If your website pages still doesn't work in the subfolder, add this code to nginx/sites-available/website.conf file too:
location /site2 {
rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}
I've tried the same installation of PrestaShop (ver 1.7.5.1) with Apache and nginx (copying the nginx installation into Apache root dir).
When I surf on admin panel I see urls like this:
http://localhost/admin825pqjv9z/index.php/configure/advanced/system-information/?_token=lJz8rH0rLWJJsrgY6tC97KuCrniEs2eps41UEoU5vqY
where index.php is concatenated with the rest of the url
When I use Apache every work fine however with Nginx, I'm getting redirected.
I've tried the following nginx configuration for PrestaShop:
https://gist.github.com/vicobits/86804fa5f6bd9e2a38f353518563590f
but it did not worked.
I just tried the example you provided on my nginx install and it went well.
Did you replace /admin-dev/ by your own admin folder name /admin825pqjv9z/? You should have:
location /admin825pqjv9z/ { # Change this for your admin url
if (!-e $request_filename) {
rewrite ^/.*$ /admin825pqjv9z/index.php last;
}
}
I hope this helps, otherwise I'd be happy to check other options with you.
I have a wordpress site running at the root of my domain. e.g http://www.example.com as I needed site to run www url I had changed the siteurl in wp-options table to http://www.example.com now when I try to visit non www url it redirects me to www variation. this is all working fine
But now I have created a subfolder 'app' there which serves an angular app. Now when I visit http://example.com/app/ then it works.. but when I visit http://www.example.com/app/ it shows me wordpress 404 page. is it not possible to have this setup?
Server is aws + apache.
If you're using an .htaccess file you can add this before the rewrite rule for wordpress index.php
RewriteCond %{REQUEST_URI} ^/app/.*$
RewriteRule ^(.*)$ $1 [L]
It checks if the requests start with app/ and pass it without modification.
Please check angular app where to serve, means which port on run and also check your main wordpress site where?