简体   繁体   English

如何使 wordpress 重写规则在子目录下工作?

[英]How can I make the wordpress rewrite rules work under a subdirectory?

I want to redirect certain URLs coming into a wordpress site to another directory outside the document root.我想将进入 wordpress 站点的某些 URL 重定向到文档根目录之外的另一个目录。 It seems the simplest way to do that would be to make wordpress a subdirectory, but then how can I get its rewrite rules to redirect to this subdirectory?似乎最简单的方法是让 wordpress 成为一个子目录,但是我怎样才能让它的重写规则重定向到这个子目录?

These are the standard rules in the apache configuration (not .htaccess)这些是 apache 配置中的标准规则(不是 .htaccess)

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Since now I want to prepend wordpress to the directory, I changed it as follows由于现在我想将 wordpress 添加到目录中,因此我将其更改如下

RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]  

But I'm getting但我得到

[perdir /] internal redirect with /wordpress/index.php [INTERNAL REDIRECT]

instead.反而。 Why?为什么? I had initially trying prepending the "wordpress" manually throughtout the rules, but apparently %{REQUEST_FILENAME} stores the initial name and not the rewritten one.我最初尝试在整个规则中手动添加“wordpress”,但显然 %{REQUEST_FILENAME} 存储初始名称而不是重写的名称。

I ended up doing it the way I suggested at the end of my query, not using RewriteBase at all.我最终按照我在查询结束时建议的方式进行了操作,根本没有使用 RewriteBase。

RewriteRule ^index\.php$ /wordpress/index.php [L]
#this rule prepends wordpress to everything
RewriteRule "^(.*)$" /wordpress/$1

#now the files have wordpress prepended, but without directory context, these conditions compare against the whole path 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
# we exclude files with wordpress prepended
RewriteRule ^((?!wordpress).*)$ /wordpress/index.php [L]

I mentioned briefly that REQUEST_FILENAME wasn't working and I figured out why by upping the LogLevel to rewrite:trace6.我简要提到了 REQUEST_FILENAME 不起作用,我通过将 LogLevel 提高到 rewrite:trace6 来找出原因。 It confirmed that REQUEST_FILENAME is getting updated by RewriteRules but without the directory context (that is, ), it was expecting a full path to the file/directory.它确认 REQUEST_FILENAME 正在由 RewriteRules 更新,但没有目录上下文(即 ),它期望文件/目录的完整路径。 So I fixed that by prepending %{DOCUMENT_ROOT} and now the code works.所以我通过添加 %{DOCUMENT_ROOT} 来解决这个问题,现在代码可以工作了。

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

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