问题描述
我正在运行一个AWS中型LAMP实例.我有三个Beta版测试的Wordpress网站都正在运行并呈现页面,但都没有100%完成.服务器软件版本为:
I am running an AWS medium LAMP instance. I have three beta testing Wordpress sites all are running and rendering pages, none are 100% complete. Server software versions are:
php 7.0.3
php 7.0.3
httpd apache/2.4.33
httpd apache/2.4.33
Wordpress 4.9.8
Wordpress 4.9.8
我已经花了很多时间研究自己的特殊挑战,包括编写一些插件代码以尝试理解问题,我已经接近了,但还没有破解问题.
I have spend considerable time researching my particular challenge, including writing some plugin code to attempt understanding the problem, I'm getting close but have not cracked the problem just yet.
我遇到的问题是,当我将永久链接设置更改为%postname%时,站点无法找到页面.在此阶段,我仅引用页面,而不引用帖子.我还没有对帖子进行任何测试.
The issue I am experiencing, is when I change my permalink settings to %postname% the site is unable to locate the pages. At this stage I am exclusively referencing only pages and not posts. I have not done any testing with posts.
我已经确认使用加载了mod_rewrite
I have confirmed that mod_rewrite is loaded using
sudo apachectl -t -D DUMP_MODULES
and
<?php phpinfo(); ?>
第一个命令列出了rewrite_module(共享). php脚本显示mod_rewrite是已加载模块的一部分.
The first command lists rewrite_module (shared) as present. The php script shows mod_rewrite as being part of the Loaded modules.
我关注的一个很好的资源是 https://serverfault.com/questions/445599 /enabling-mod-rewrite-on-amazon-linux .还有很多其他人,但这似乎掌握了所有其他搜索所提出的要点.
A good resource I followed was https://serverfault.com/questions/445599/enabling-mod-rewrite-on-amazon-linux. There were numerous others but this seemed to grasp all the salient points, that other searching brought up.
从上面的内容中,我接受了有关测试重定向的建议,该建议有效,将我的一个域路由到wordpress.org. (为实现此测试,我编辑了/etc/httpd/conf/httpd.conf)
From the above I took the advice on testing redirection and it worked, routing one of my domains to wordpress.org. (to achieve this test i edited /etc/httpd/conf/httpd.conf)
我还创建了一个基本插件来测试各种WordPress功能.我认为某些内部对齐方式可能导致WordPress无法找到请求的页面.这是错误的,WordPress在将页面ID传递到get_permalink()函数时找到了正确的页面.
Also I have created a basic plugin to test various WordPress functions. I thought that some internal misalignment might have caused WordPress not to find the requested page. This was wrong WordPress found the correct page when passing the page ID into the get_permalink() function.
我只是添加了一个回声get_permalink(290);到我的简单插件代码.正确地呼应了
I simply added an echo get_permalink(290); to my simple plugin code. And this correctly echoed
https://www.mydomainname.com/correct-slug-for-page-290
如果我复制并粘贴URL失败,则失败.
If I copy and paste the URL above it failed.
注意:当我修改永久链接设置时,.htaccess文件会自动修改/更新.
Note: When I modify my permalink settings the .htaccess file is automatically modified/updated.
如果我将WordPress永久链接设置设置为纯文本,则一切正常.
If I set my WordPress permalink settings to plain, everything works.
如果我将WordPress永久链接设置设置为custom并使用index.php/%postname%/所有链接都可以解决. (同时存在和不存在.htaccess文件)
If I set my WordPress permalink settings to custom and use index.php/%postname%/ all links resolve. (Both with and without .htaccess file present)
如果我将永久链接设置仅设置为%postname%,则所有内容都会出现404错误.设置为%postname%时,wordpress会自动创建以下文件
If I set my permalink settings to only %postname% everything becomes a 404 error. On setting to %postname% wordpress automatically creates the following file
-rw-r--r-- 1 apache www 235 Sep 4 21:47 .htaccess
#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
我的httpd.conf中包含大部分注释的摘录被删除了.我确实计划返回并收紧一些设置,一旦我可以使用此基本功能.
Snippets from my httpd.conf with most of the comments are edited out. I do plan on going back and tightening up some of the settings once I can get this base functionality to work.
赞赏任何见解,技巧或建议.谢谢大家.
Appreciate any insights, tips or suggestions. Thanks everyone.
#
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride All
Require all granted
</Directory>
DocumentRoot "/var/www/html"
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride All
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ".ht*">
Require all denied
</Files>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
EnableSendfile on
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/beta-test-one.com
ServerName beta-test-one.com
ServerAlias www.beta-test-one.com
RewriteEngine On
<IF "req('Host') != 'www.beta-test-one.com'">
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=301]
</IF>
<ELSE>
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=301]
</ELSE>
ErrorLog logs/beta-test-one.com-error_log
CustomLog logs/beta-test-one.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/beta-test-two.com
<Directory "/var/www/html/beta-test-two.com">
AllowOverride All
</Directory>
ServerName beta-test-two.com
ServerAlias www.beta-test-two.com
RewriteEngine On
<IF "req('Host') != 'www.beta-test-two.com'">
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=301]
</IF>
<ELSE>
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=301]
</ELSE>
ErrorLog logs/beta-test-two.com-error_log
CustomLog logs/beta-test-two.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/beta-test-three.com.au
ServerName beta-test-three.com.au
ServerAlias www.beta-test-three.com.au
RewriteEngine On
<IF "req('Host') != 'www.beta-test-three.com.au'">
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=301]
</IF>
<ELSE>
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=301]
</ELSE>
ErrorLog logs/beta-test-three.com-error_log
CustomLog logs/beta-test-three.com-access_log common
</VirtualHost>
推荐答案
问题已解决.
我开始查看我的mod_rewrite设置.我意识到我已经从httpd.conf文件中进行了测试,但是没有按照以下文章中的一些技巧在.htaccess级别上进行重定向测试,
I started reviewing my mod_rewrite setup. I realized that I had tested from within the httpd.conf file but not tested redirection at the .htaccess level following some tips from the following article,
最终,绕过了.htaccess文件.
It ended up that the .htaccess file was being bypassed.
最终的罪魁祸首是httpd.conf文件中对HTTP的显式重定向
The ultimate culprit were the explicit re-directs to https in the httpd.conf file
RewriteEngine On <IF "req('Host') != 'www.beta-test-three.com.au'">
RewriteCond %{HTTP:X-Forwarded-Proto} =http RewriteRule .*
https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=301] </IF> <ELSE>
RewriteCond %{HTTP:X-Forwarded-Proto} =http RewriteRule .*
https://%{HTTP:Host}%{REQUEST_URI} [L,R=301] </ELSE>
我删除了这些行并再次进行了测试……标准的WordPress永久链接都已解决.我最初添加这些行是因为当我输入domain-name.com(不带https前缀)时,我收到的浏览器消息表明该站点不安全.
I removed these lines and tested again… The standard WordPress permalinks are all resolving. I had initially added those lines because when I had been typing domain-name.com (without the https prefix) I was getting browser messages that the site is not secure.
但是,通过如下更新wp-config文件(见下文),我不需要httpd.conf文件中的https重定向,这使得.htaccess重写规则可以正常工作.
However by updating the wp-config file as follows (see below) I don’t need the https redirection in the httpd.conf file and this has allowed the .htaccess rewrite rules to work.
define('WP_HOME','https://www.domain-name.com');
define('WP_SITEURL','https://www.domain-name.com');
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';
这篇关于WordPress的永久链接行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!