I am running wordpress in a sub-directory /wordpress, I want it to be served at root. I cannot change the application root to /wordpress, because i have a core php application at root, which runs as example.com/some_file.php . My configuration file run wordpress homepage and other pages fine, but wp-admin pages are broken, also any images put in wp-content folder are broken when called from within the theme. My configuration file for rewrites is:
location = / {
try_files $uri $uri/wordpress /wordpress/index.php?$args;
}
location /wordpress {
index index.php;
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php;
}
code
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
The following serves the html root folder (/var/www/html) to the domain subdirectory (example.com/wordpress).
Tested on Ubuntu 18.04, PHP 7.3 & latest nginx.
location ^~ /wordpress {
# Sets /var/www/html as root of example.com/wordpress
alias /var/www/html;
# Get script /var/www/html/index.php
# It's important to start with a slash "/",
# that way it looks for index.php at the root directory,
# not in example.com/wordpress/some-slug/index.php
index /index.php;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
# First attempt to serve request as file, then
# as directory, then fall back to index.php
try_files $uri $uri/ /index.php?$args;
}
# For security reasons, set php settings at root level.
# This prevents root level php files from showing as plain text.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
Ignore the top answer, it doesn’t work with /wp-login.php.
Here’s a better approach
location / {
rewrite ^ /wordpress/ permanent;
}
location ^~ /wordpress {
# Sets /var/www/html as root of example.com/wordpress
alias /var/www/html;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
# First attempt to serve request as file, then
# as directory, then fall back to index.php
try_files $uri $uri/ /index.php?$args;
# For security reasons, set php settings at root level.
# This prevents root level php files from showing as plain text.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^/wordpress(/.+\.php)(.*)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
You can do the below for WP in a sub-folder:
location /wordpress {
rewrite ^(/[^/]+)?(/wp-.*) /wordpress/$2 break;
rewrite ^/wordpress/(.*)$ /wordpress/index.php?q=$1 last;
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.