.htaccess重定向不再起作用

时间:2018-11-09 作者:Marj Wyatt

2016年5月,我使用.htaccess. 客户端验证了所有.htaccess 在我们结束项目之前,重定向的工作让他们满意。

本周,客户通知我这些重定向不再有效。他们不知道这是什么时候开始失败的,经过几个小时的研究,我找不到他们不继续工作的任何原因。

客户端不想为此使用插件。

我包括.htaccess 提交审查和建议。

# Disable XMLRPC
<Files "xmlrpc.php">
Order Deny,Allow
Deny from all
</Files>
# End Disable XMLRPC

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Redirects
# Change "www" to "www" when moved to live
RedirectMatch 301 ^/watermeloncakes/ //www.clienturl.com/watermelon-cakes-1/
RedirectMatch 301 ^/watermelon-cakes/ //www.clienturl.com/watermelon-cakes-1/
RedirectMatch 301 ^/edible-arrangements-coupons/ //www.clienturl.com/fruit-arrangements-coupons/
RedirectMatch 301 ^/edible-arrangements/ //www.clienturl.com/fruit-arrangements/
RedirectMatch 301 ^/edible-arrangements-5-reasons/ //www.clienturl.com/fruit-arrangements-5-reasons/
RedirectMatch 301 ^/candy/ //www.clienturl.com/blog/christmas-candy-platters
RedirectMatch 301 ^/candy //www.clienturl.com/blog/christmas-candy-platters
RedirectMatch 301 ^/pumpkin-carving-instruction-ray-villafane/ //www.clienturl.com/blog/shop/ray-villafanes-3d-pumpkin-carving/
RedirectMatch 301 ^/4thofjulyideas/ //www.clienturl.com/4th-of-july-ideas/
RedirectMatch 301 ^/pumpkin-carving-tattoos/ //www.clienturl.com/blog/shop/pumpkin-carving-patterns-tattoos/
RedirectMatch 302 ^/transfer-pattern-paper-carving-pumpkins-watermelons/ //www.clienturl.com/blog/product-category/patterns-and-transfer-papers/
RedirectMatch 302 ^/u-and-v-fruit-carving-tools/ //www.clienturl.com/blog/shop/u-v-fruit-carving-tools/
RedirectMatch 302 ^/fruit-carving-knife-set/ //www.clienturl.com/blog/shop/kom-kom-fruit-carving-knife-set/
RedirectMatch 302 ^/thai-pro-fruit-carving-knife/ //www.clienturl.com/blog/shop/thai-pro-carving-knife/
RedirectMatch 302 ^/13-pc-fruit-carving-tools/ //www.clienturl.com/blog/shop/pro-fruit-carving-tools-13-piece/
RedirectMatch 302 ^/corrugated-u-cutters-and-melon-baller/ //www.clienturl.com/blog/shop/corrugated-u-cutters/
RedirectMatch 301 ^/shop/ //www.clienturl.com/blog/shop/
RedirectMatch 301 ^/pp-resources/ //www.clienturl.com/blog/pumpkin-portraits-resources-members/
RedirectMatch 301 ^/dvd-lessons-and-tool-sets/ //www.clienturl.com/blog/shop/
RedirectMatch 301 ^/message-sent-success/ //www.clienturl.com/blog/your-message-successfully-sent/
RedirectMatch 301 ^/which-lessons-tools-for-you/ //www.clienturl.com/blog/best-lessons-for-you/
RedirectMatch 301 ^/fabric //www.clienturl.com/blog/shop/pattern-transfer-fabric/
RedirectMatch 301 ^/101 //www.clienturl.com/vegetable-and-fruit-carving-course-101
RedirectMatch 301 ^/weave //www.clienturl.com/blog/shop/melon-basket-weave/
RedirectMatch 301 ^/twirl //www.clienturl.com/blog/shop/twisty-twirl-tool/
RedirectMatch 301 ^/vegcurl //www.clienturl.com/blog/shop/vegetable-curler/
RedirectMatch 301 ^/wavy //www.clienturl.com/blog/shop/wavy-peeler/

#IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

#<Limit GET POST>
#The next line modified by DenyIP
#order allow,deny
#The next line modified by DenyIP
#deny from all
#allow from all
#</Limit>
#<Limit PUT DELETE>
#order deny,allow
#deny from all
#</Limit>
#AuthName vegetablefruitcarving.com

# stop hotlinking and serve alternate content
# <IfModule mod_rewrite.c>
# RewriteEngine on
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?vegetablefruitcarving\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?vegetablefruitcarving\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?sbox\\.vegetablefruitcarving\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?twitter\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?twitter\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?facebook\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?facebook\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?pinterest\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?pinterest\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?youtube\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?youtube\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?flickr\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?flickr\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^http://(www\\.)?plus.google\\.com/.*$ [NC]
# RewriteCond %{HTTP_REFERER} !^https://(www\\.)?plus.google\\.com/.*$ [NC]
# RewriteRule .*\\.(gif|jpg|png|pdf)$ //altlab.com/hotlinking.html [R,NC,L]
# </ifModule>

<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from 79.29.221.71

# BEGIN WPSuperCache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#If you serve pages from behind a proxy you may want to change \'RewriteCond %{HTTPS} on\' to something more sensible
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{HTTPS} on
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html.gz -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html.gz" [L]

RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{HTTPS} !on
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html.gz -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html.gz" [L]

RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTPS} on
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html" [L]

RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\\"]+ [NC]
RewriteCond %{HTTPS} !on
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html" [L]
</IfModule>

# END WPSuperCache

# 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

2 个回复
SO网友:MrWhite

RedirectMatch 301 ^/watermeloncakes/ //www.clienturl.com/watermelon-cakes-1/
(I假设www.clienturl.com 是此站点的主机名。)

好吧,事情是这样的。。。这在任何版本的Apache上都不会起作用!?因为mod\\u alias不支持协议相关URLRedirectMatch (或Redirect) 或者为这件事重写mod\\u。

Apache docs:

新URL可以是以方案和主机名开头的绝对URL,也可以是以斜杠开头的URL路径。在后一种情况下,将添加当前服务器的方案和主机名。

协议相关URL,如//www.clienturl.com/watermelon-cakes-1/ 将被视为根相对(“以斜杠开头的URL路径”),因此上述指令将导致格式错误的重定向到:

https://www.clienturl.com//www.clienturl.com/watermelon-cakes-1/
有可能是另一个重定向后来纠正了这一点(可能是在WordPress本身中)!?但在你的.htaccess 文件

Apache在创建Location 要发送回客户端的标头。它不会发回与协议相关的URL,并期望客户端对其进行解析(如果手动为Location 服务器端脚本中的头)。

如果这些指令有效,那么看起来有人删除了协议(https:http:?) 从目标URL,或添加主机名(//www.clienturl.com)? 是否可能这些最初被重定向到http: 有人认为他们会试图通过使其“协议相关”来避免“双重重定向”(因为您正在强制HTTPS)?

上述指令需要使用完整的绝对URL:

RedirectMatch 301 ^/watermeloncakes/ https://www.clienturl.com/watermelon-cakes-1/
或使用根相对URL:

RedirectMatch 301 ^/watermeloncakes/ /watermelon-cakes-1/
使用mod\\u rewrite代替RewriteRule 对于这些重定向,而不是mod\\u别名RedirectMatch (或Redirect) 指令。这仅仅是因为您应该避免混合来自两个模块的重定向/重写,并且您已经在整个过程中使用了mod\\u rewrite.htaccess WordPress本身使用mod\\u rewrite来实现其前端控制器模型。

例如:

RewriteRule ^watermeloncakes/ /watermelon-cakes-1/ [R=301,L]
(无需重复RewriteEngine 指令。)

Apache模块在整个请求过程中独立地、在不同的时间执行。mod\\u rewrite在mod\\u alias之前执行,而不管.htaccess 文件例如,您可以将所有RedirectMatch 指令,它不会改变执行顺序。

因此,通过混合来自这两个模块的重定向,最终可能会出现意外的冲突,因为mod\\u重写始终优先,而不管顺序如何。而且,所有mod\\u rewrite指令都是在mod\\u别名重定向之前首先处理的-每个请求都会处理所有内容(无论是否有重定向)。如果使用mod\\u rewrite,则会立即发生重定向,并绕过其余指令。

但是,由于所有这些重定向看起来似乎只是在更正URL,否则会触发404的URL,因此最好在服务器端代码(即WordPress)中执行这些重定向。例如,一旦WordPress确定URL将导致404,然后检查您的重定向。这样做可以避免任何重定向代码“减慢”正常的站点使用。

SO网友:Remzi Cavdar

启用Apache mod_rewrite module.
并启用ReWriteEnginemod_rewrite module.<下面是如何使用重写规则的示例:

# Turning on the rewrite engine is necessary for the following rules and
# features. "+FollowSymLinks" must be enabled for this to work symbolically.

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on

RewriteRule ^panda$ /tiger [R=301,L]
RewriteRule ^house$ /car [R=301,L]
RewriteRule ^monkey$ /deer [R=301,L]
</IfModule>
还有其他一些建议,我知道你在用WP Super Cache 我想推荐WP Fastest Cache 这是最好的免费缓存插件之一。请参见:https://wordpress.org/plugins/wp-fastest-cache/

如果你的主题支持这一点,你可以将其与Fast Velocity Minify 请参见:https://wordpress.org/plugins/fast-velocity-minify/

通过这种组合,我们公司website 倾向于在gtmetrix上获得近乎完美的100分。com,使用WP Fastest CacheFast Velocity Minify

另外,保持简单,还有一个插件叫做Redirection 谁在做你想做的事情。我强烈推荐此插件用于任何重定向:https://wordpress.org/plugins/redirection/

结束

相关推荐

WordPress没有生成.htaccess,但告诉我(更新了固定链接结构)。

我希望有人能帮助我。我正在使用directadmin配置新的Centos 7服务器,遇到以下问题:;我的。保存permalinks后不会生成htaccess文件,但会显示(Permalink结构已更新)</尝试保存站点后,多站点wordpress给出错误500(日志中没有错误)。我认为这与同样的问题有关,为什么我的htaccess不会生成。htaccess似乎是我问题的一个更容易的起点</你需要知道的事情;在Wordpress中,其他一切似乎都起作用了。我甚至可以在wordpress中下载、安