简体   繁体   中英

Setting .htaccess for Wordpress in a subdirectory with 'Post name' permalinks

I am trying to set up a Wordpress installation, where Wordpress is in a subdirectory, but where the subdirectory is always rewritten out of the URL.

I've been trying to follow a whole bunch of other questions / posts about doing this, but no one else's solutions seem to work for me, and a lot of the time they just get the pages to load, without removing the subdirectory from the URL, which is crucial for me.

I'm also hoping for a solution that will work the same locally ( http://localhost/wordpress ) as it will on a live server ( http://example-site.com ). However, I'll explain the problem from a local point of view:

My root folder is structured like this:

.htaccess
wp /* contains all wordpress core files */
wp-content /* custom wp-content folder, which is set to be pointed at in my wp-config */
wp-config.php

So, if we ignore the .htaccess, the site would be accessed by going to http://localhost/wordpress/wp , and the goal is to access it by just going to http://localhost/wordpress .

This was achieved using this .htaccess:

RedirectMatch 302 /wp/$ /wordpress/$1

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /wordpress/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ wp/$1
    RewriteRule ^(/)?$ wp/index.php [L]
</IfModule>

However, at this point, my wordpress installation was set to use default permalinks, which create pages like http://localhost/wordpress?p=12 . I need to set this to a different permalink type, which uses URL segments rather than query parameters, like http://localhost/wordpress/hello-world . Once that setting is changed, I can no longer access other pages with that .htaccess (the home page still works fine though)

I tried using this .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^index\.php$ -
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wordpress/wp/$1 [L]
</IfModule>

Which now causes the sub pages to load, though the home page no longer does, nor do some asset files, like http://localhost/wordpress/wp-includes/css/admin-bar.min.css

I can't seem to find a solution that makes everything work at the same time!

I'd also like to mention that I have a very small understanding of writing .htaccess files, so if anyone is going to answer this, please explain things like I am an idiot, as I may not understand you otherwise.

I am considering that you have redirected the domain to the relavant folder, or on shared hosting you have setup the domain to point to your subfolder. I am also considering that you are adding the htaccess to the root. Here is code from the official codex for your situation.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ blog [L]

Replace yourdomain.com and blog with website address and directory. Delete everything else from the file. Then goto settings > permalink and change the permalink to what ever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM