Mobile app version of vmapp.org
Login or Join
Speyer207

: 500 error when using a "Redirect 301" rule in htaccess 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&

@Speyer207

Posted in: #Apache #Htaccess

I am trying to redirect an old URL
example.com/keyboard/keyboardchart.php?gam=7&sty=15&lay=1

to a new URL
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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Speyer207

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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.

10% popularity Vote Up Vote Down


 

@Yeniel560

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme