Matt
Matt

Reputation: 136

Redirect overwrites different rules in .htaccess

I want to redirect (301) 900 URLs. I carefully made the whole list, without errors, but is behaves like a string replace. Take for example this line:

Redirect    301 /assortiment/artikelen_ingedeeld_per_branche/transporteurs_verhuisbedrijven3/Verhuisdekens____baal_a_25_stuks_.html?id=979 http://example.com/221-transport-en-verhuisbedrijven

And there is this line:

Redirect    301 /assortiment/   http://example.com/alle-producten

When I visit /assortiment/artikelen_ingedeeld_per_branche/transporteurs_verhuisbedrijven3/Verhuisdekens____baal_a_25_stuks_.html?id=979 then it redirects to /alle-productenartikelen_ingedeeld_per_branche/transporteurs_verhuisbedrijven3/Verhuisdekens____baal_a_25_stuks_.html?id=979

Notic the assortiment --> alle-producten replacement at the beginning. As if it is rewritten like 'str_replace' I see what is happening, but I simply don't understand .htaccess well enough.

Upvotes: 0

Views: 108

Answers (1)

Jon Lin
Jon Lin

Reputation: 143876

The first redirect does nothing. You can't put the query string in a Redirect. The second is working as intended. The way Redirect works, is sort of a "starts with" approach. So if the directive is:

Redirect /a /b

then:

  • /a will redirect to /b
  • /a/ will redirect to /b/
  • /abcd123 will redirect to /bbcd123
  • /a/b/c/d will redirect to /b/b/c/d

Additionally, if your rule is:

Redirect /a/ /b

then:

  • /a/ will redirect to /b
  • /a/b/c/d will redirect to /bb/c/d

etc.

If you don't want this to happen, then use redirect match:

RedirectMatch ^/assortiment/$   http://example.com/alle-producten

However, this isn't going to help with your query string. Neither Redirect or RedirectMatch will match against the query string. You'll need to use mod_rewrite and rewrite rules:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^id=979$
RewriteRule ^assortiment/artikelen_ingedeeld_per_branche/transporteurs_verhuisbedrijven3/Verhuisdekens____baal_a_25_stuks_.html$ http://example.com/221-transport-en-verhuisbedrijven [L,R=301]

RewriteRule ^assortiment/   http://example.com/alle-producten [L,R=301]

Upvotes: 5

Related Questions