4

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?

2
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.

  • Fixed RewriteRule substitution string. Added info about query string. – MrWhite Dec 18 '16 at 17:03
-1

Quick and dirty way - try, it should work.

Redirect 301 /keyboard/keyboardchart.php?gam=7&sty=15&lay=1 /keyboard/keyboard-chart.php?gam=7&sty=15&lay=1

If You have a lot of such URL then look forward using rewrite rules.

  • 2
    The Redirect directive matches against the URL-path only, not the query string. If you specify a query string then it will never match (as it tries to match it against the URL-path). Just remove the query string and it should work. – MrWhite Dec 18 '16 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.