大网数据广告活动
网站运营与故障问答

500 error when using a "Redirect 301" rule in htaccess

· 52CE 我爱测

I am trying to redirect an old URL http://example.com/keyboard/keyboardchart.php?gam=7&sty=15&lay=1 to a new URL http://example.com/keyboard/keyboard-chart.php?gam=7&sty=15&lay=1 using .htaccess . Here is the contents of example.…

问题原文

原帖链接 · posfan12 · CC BY-SA 3.0

I am trying to redirect an old URL

http://example.com/keyboard/keyboardchart.php?gam=7&sty=15&lay=1

to a new URL

http://example.com/keyboard/keyboard-chart.php?gam=7&sty=15&lay=1

using .htaccess.

Here is the contents of example.com/keyboard/.htaccess:

IndexIgnore *
Redirect 301 keyboardchart.php keyboard-chart.php

However, I get a "500 Internal Server Error" when navigating to the old URL.

What am I doing wrong?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Redirect 301 keyboardchart.php keyboard-chart.php

The 500 error is because the target URL is relative. That is not allowed with a mod_alias Redirect. The target URL must either be absolute (with a scheme and hostname) or must start with a slash (ie. root-relative).

But also, the source URL-path will not match either. You must specify a root-relative URL-path, starting with a slash.

Regardless of where the .htaccess file is located, the mod_alias Redirect directive is the same. Unlike mod_rewrite (RewriteRule) that has the concept of a "directory-prefix".

So, like VladShundalov suggests, you would need a directive of the form:

Redirect 301 /keyboard/keyboardchart.php /keyboard/keyboard-chart.php

Note that this matches any query string. The query string is automatically passed through to the target. You can't actually match the query string with a mod_alias Redirect.

If you need to match the specific query string then you must use mod_rewrite instead. For example, in the example.com/keyboard/.htaccess file you could write something like:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^gam=7&sty=15&lay=1$
RewriteRule ^keyboardchart\.php$ /keyboard/keyboard-chart.php [R=301,L]

Note that you don't state the subdirectory on the RewriteRule pattern in this case, however, you do still need a root-relative path on the substitution (unless you specify the path with a RewriteBase directive). The query string is passed through to the substitution automatically by default.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-12-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。