WP in subfolder hidden by htaccess, but media links show the subfolder
-
10-03-2021 - |
Question
The WordPress install is in /public_html/example
. In the .htaccess
file in public_html
I have this code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
In /public_html/example
I have exactly the same file.
So when I access the site, all the pages have URLs such as http://example.com/about
. The URL rewriting specified in .htaccess
is working. But the rewriting does not work for media files, which all have links like http://example.com/example/wp-content/uploads/2018/01/hello.txt
.
My question: Is it possible to get file links such as http://example.com/wp-content/uploads/2018/01/hello.txt
(without the duplication of "example")?
Solution
One option is to explicitly move the wp-content folder one level up so it resides at /public_html/wp-content
by defining WP_CONTENT_DIR
in wp-config.php
.
For this I'd recommend something more like the second method described in Giving WordPress Its Own Directory. Editing wp-config.php has an example for the WP_CONTENT_DIR
change.
The resulting directory structure (relative to public_html
would look like this:
- .htaccess
- index.php
- wp-content
- ...
- example
- wp-admin
- wp-includes
- ...
Bonus:
You can use a .htaccess
file similar to this one:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) example/$1 [L] # your WP folder
RewriteRule ^(.*\.php)$ wp/$1 [L]
RewriteRule . index.php [L]
This way you can set both Home URL and Site URL to https://example.com
(without /example
, so it won't be clear at first glance that WordPress is installed in a subfolder. You can then log in via https://example.com/wp-admin/
Such a setup is especially useful when using Composer, where WordPress is best installed in a subdirectory like described in Rarst's Composer in WordPress guide.