htaccess 重定向 301 问题.. 所有使用一个字符串的重定向都无法重定向并出现 404

发布于 2024-08-27 01:06:53 字数 1399 浏览 3 评论 0原文

所以我移动了一个网站,并尝试对所有内容进行 301 重定向,我经常这样做,所以这是一个奇怪的问题,但可能是我没有看到的愚蠢问题。

我的所有重定向都工作正常,除了第一个字符串以“/Dining”或“/dining”开头的任何重定向都失败。例如,此重定向工作正常 -

Redirect 301 /healthfitness/teeth.cfm /healthcare/pretty-teeth

...以及数百个其他重定向。

但所有这些都失败了(比我展示的要多)-

Redirect 301 /Dining/diningreviews/vawines.cfm /shopping/wines-2004
Redirect 301 /Dining/diningathome/carrotcake.cfm /home-garden/carrot-cake-2003
Redirect 301 /Dining/diningathome/oldvarolls.cfm /home-garden/virginia-rolls-2003
Redirect 301 /Dining/diningathome/pumpkincake.cfm /home-garden/pumpkin-cake-2003

我的 .htaccess 文件的顶部看起来像这样-

RewriteEngine On
RewriteBase /

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>

#Everything below here are Redirect 301s

So I have moved a website and am trying to 301 redirect everything, which I do quite often so this is a weird problem but probably something stupid I'm not seeing.

ALL of my redirects are working fine, except any redirect that the first string starts with "/Dining" or "/dining" are failing. For example, this redirect works fine-

Redirect 301 /healthfitness/teeth.cfm /healthcare/pretty-teeth

...as well as 100s of others.

But all of these are failing (many more than I'm showing)-

Redirect 301 /Dining/diningreviews/vawines.cfm /shopping/wines-2004
Redirect 301 /Dining/diningathome/carrotcake.cfm /home-garden/carrot-cake-2003
Redirect 301 /Dining/diningathome/oldvarolls.cfm /home-garden/virginia-rolls-2003
Redirect 301 /Dining/diningathome/pumpkincake.cfm /home-garden/pumpkin-cake-2003

The top of my .htaccess file looks like this-

RewriteEngine On
RewriteBase /

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>

#Everything below here are Redirect 301s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心病无药医 2024-09-03 01:06:53

重定向语句是否必须在目标中包含协议?

Dont redirect statements have to include the protocol in the destination?

你与清晨阳光 2024-09-03 01:06:53

您必须在重定向中包含域名。

切勿在同一文件中混合 mod_alias 和 mod_rewrite 指令。如果您对任何规则使用 RewriteRule,则必须对所有规则使用 RewriteRule(而不是 RedirectRedirectMatch)你的规则。

在任何重写(使用 RewriteRule 语法)之前列出所有重定向(使用 RewriteRule 语法),否则您将无意中将重写的指针作为新 URL 在网络上公开。

您当前使用的代码效率非常低。 .* 模式意味着您的 .htaccess 文件将为每个到达服务器的 URL 请求尝试数十万次“后退并重试”尝试匹配。

特别是,

^(.\*/)? 应该在两个地方替换为 ^([^/]+/)\*

^.\*/ 应替换为 ^/([^/]+/)\*

!.\*wp-content/plugins.\* 应替换为 !wp-content/plugins

^([_0-9a-zA-Z-]+/)?(.\*\\.php)$ 应替换为 ^([_0-9A-Z-]+/)?([^.]+\\.php)$ 以及 [NC]<还添加了 /code> 标志。

“向 /wp-admin 添加尾部斜杠”重定向应该是文件中的第一个规则集,并且目标 URL 应包含协议和域名。该规则后应立即遵循非 www 到 www 的规范化规则。

新代码的运行速度可能会快数百倍。

You must include the domain name in the redirect.

Never mix mod_alias and mod_rewrite directives in the same file. If you use RewriteRule for any of your rules, you must use RewriteRule (not Redirect or RedirectMatch) for ALL of your rules.

List all redirects (using RewriteRule syntax) before any of the rewrites (using RewriteRule syntax) otherwise you will inadvertently expose rewritten pointers back out on the the web as new URLs.

The code you are currently using is very very inefficient. The .* patterns mean your .htaccess file will attempt hundreds of thousands of "back off and retry" trial matches for every URL request hitting the server.

In particular,

^(.\*/)? should be replaced by ^([^/]+/)\* in two places.

^.\*/ should be replaced by ^/([^/]+/)\*

!.\*wp-content/plugins.\* should be replaced by !wp-content/plugins

^([_0-9a-zA-Z-]+/)?(.\*\\.php)$ should be replaced by ^([_0-9A-Z-]+/)?([^.]+\\.php)$ with the [NC] flag also added.

The "add a trailing slash to /wp-admin" redirect should be the very first ruleset in the file and the target URL should include the protocol and domain name. That rule should be immediately followed with a non-www to www canonicalisation rule.

The new code may well run hundreds of times quicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文