问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
