简体   繁体   English

在带有“帖子名称”永久链接的子目录中为Wordpress设置.htaccess

[英]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. 我正在尝试设置Wordpress安装,其中Wordpress在子目录中,但是始终从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. 我一直在尝试着其他有关此操作的问题/帖子,但是似乎没有其他人的解决方案对我有用,而且很多时候他们只是在不删除子目录的情况下加载页面。 URL,对我来说至关重要。

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 ). 我还希望有一个与本地服务器( http://example-site.com )相同的本地解决方案( http://localhost/wordpress )。 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 . 因此,如果我们忽略.htaccess,则可以通过访问http://localhost/wordpress/wp来访问该站点,而目标是通过仅访问http://localhost/wordpress来对其进行访问。

This was achieved using this .htaccess: 这是使用以下.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 . 但是,此时,我的wordpress安装被设置为使用默认的永久链接,该链接会创建类似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 . 我需要将其设置为不同的永久链接类型,该类型使用URL段而不是查询参数,例如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) 更改该设置后,我将无法再使用该.htaccess访问其他页面(尽管主页仍然可以正常工作)

I tried using this .htaccess: 我尝试使用此.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 现在,这将导致子页面加载,尽管主页不再加载,某些资产文件也不再加载,例如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. 我还要提及的是,我对编写.htaccess文件了解甚少,因此,如果有人要回答这个问题,请解释一下我是个白痴之类的事情,否则我可能无法理解您。

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. 我也在考虑您要将htaccess添加到根目录。 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. 用网站地址和目录替换yourdomain.com和博客。 Delete everything else from the file. 从文件中删除其他所有内容。 Then goto settings > permalink and change the permalink to what ever you want. 然后转到设置>永久链接并将永久链接更改为所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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