wordpress - Apache 301 重定向原始 URL 包含特殊字符的位置

标签 wordpress apache .htaccess mod-rewrite redirect

我正在寻找一些 .htaccess 重定向RewriteRule 帮助。

我的客户有一个网站,其网址格式不正确,Google Web Tools 将这些网址报告为 404 错误,例如:

/calendar-of-events/wanda-hollberg’s-fused-glass-jewelry-trunk-show/

注意到坏的撇号“’”字符了吗?


这是另一个例子:

/dining club.cfm

注意到空格“”了吗?


再举一个例子:

/calendar-of-events/“a-viticultural-voyage”-wine-dinnerstar/

注意到错误的引号““”和“”“吗?


我尝试过重定向规则但没有成功,例如 UTF-8 编码...

Redirect 301 /calendar-of-events/wanda-hollberg%E2%80%99s-fused-glass-jewelry-trunk-show/ /calendar-of-events/
Redirect 301 /dining%20club.cfm /cuisine/
Redirect 301 /calendar-of-events/%E2%80%9Ca-viticultural-voyage%E2%80%9D-wine-dinnerstar/ /calendar-of-events/

以及尝试使用 RegExp...

Redirect 301 /calendar-of-events/wanda-hollberg(.*)s-fused-glass-jewelry-trunk-show/ /calendar-of-events/
Redirect 301 /dining(.*)club.cfm /cuisine/
Redirect 301 /calendar-of-events/(.*)a-viticultural-voyage(.*)-wine-dinnerstar/ /calendar-of-events/

但我猜正则表达式在重定向中不起作用?!

所以我尝试了RewriteRule,但仍然没有成功......

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.*)/calendar-of-events/wanda-hollberg(.*)s-fused-glass-jewelry-trunk-show/$ /calendar-of-events/ [R=301,L]
</IfModule>

请告诉我,我错过了什么或做错了什么?

我应该使用重定向还是RewriteRule?我应该如何写出规则?


我还应该注意到,它现在位于 WordPress 网站上,因此 .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

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# redirect none-www to www
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

# redirect m. to www
RewriteCond %{HTTP_HOST} ^m\.domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

# redirect legacy. to www
RewriteCond %{HTTP_HOST} ^legacy\.domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

</IfModule>

预先非常感谢您的建议和努力。干杯。

最佳答案

您需要删除规则中的前导斜杠。如果您想匹配 /calendar 的请求,正则表达式需要以 ^calendar 开头,不带前导斜杠。当与 htaccess 文件中的规则匹配时,mod_rewrite 会删除斜杠。

就像 Justin Iurman 提到的那样,所有重定向必须位于 WordPress 路由规则之前。规则的顺序非常重要,路由规则会吞噬所有请求,从而使您的重定向中断。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# redirect none-www to www
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

# redirect m. to www
RewriteCond %{HTTP_HOST} ^m\.domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

# redirect legacy. to www
RewriteCond %{HTTP_HOST} ^legacy\.domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

RewriteRule ^calendar-of-events/wanda-hollberg`s-fused-glass-jewelry-trunk-show/$ /calendar-of-events/ [R=301,L]
RewriteRule ^dining\ club.cfm /cuisine/ [R=301,L]
RewriteRule ^calendar-of-events/\“a-viticultural-voyage\”-wine-dinnerstar/ /calendar-of-events/ [R=301,L]
</IfModule>

# 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

在 mod_rewrite 处理 URI 之前,转义序列未转义,因此您需要在正则表达式中添加实际的未转义字符。或者您可以使用 \x##,例如:

RewriteRule ^calendar-of-events/\xE2\x80\x9Ca-viticultural-voyage\E2\x80\x9D-wine-dinnerstar/ /calendar-of-events/ [R=301,L]

(.*) 分组在 Redirect 中不起作用的原因是该指令不使用正则表达式。为此,您必须使用RedirectMatch。但无论如何你想坚持使用 mod_rewrite 。由于您有 WordPress 规则,mod 重写和 mod 别名最终都会应用于相同的请求,这最终会破坏您的重定向。

关于wordpress - Apache 301 重定向原始 URL 包含特殊字符的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264669/

相关文章:

.htaccess - htaccess 301 重定向新基础域名的通配符

.htaccess 重定向 http > https 301 重定向

apache - htaccess重写

php - Wordpress 子主题文件未覆盖

javascript - 为什么 Wordpress 无法加载资源?它表示服务器响应状态为 404(未找到)

wordpress - 使用 apt-get 安装 Web 应用程序的实例

python - 使用 apache mod_wsgi 部署基于 flask 的应用程序时的 MySQL 连接

php - 将 php 代码添加到 .html 文件

wordpress - 将跨度添加到存档帖子计数

macos - SSL: 无法加载 CA 证书文件/etc/pki/tls/certs/ca-bundle.crt