Home > Mobile >  redirect rule removing slash randomly
redirect rule removing slash randomly

Time:07-08

For some reason, on some rules the / is removed. I have more rules than this but for the first one in this case it will work correctly but the second one which could be anywhere in the list of rules, redirects to https://www.example.comnot-working-url instead of https://www.example.com/not-working-url. If this happened for all it would perhaps make more sense but it only does this for some urls. Why would this be?

RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^en/(.*) /$1 [L,NE,R=301]

Redirect 301 /en/working-url https://www.example.com/working-url
Redirect 301 /en/not-working-url https://www.example.com/not-working-url

CodePudding user response:

You likely have a "catch-all" Redirect directive that is missing the trailing slash.

For example, if you have the following at the end of your rules:

Redirect 301 / https://www.example.com

Note the missing trailing trailing slash on the target URL.

Then a request for /not-working-url would be "erroneously" redirected to https://www.example.comnot-working-url by the above rule.

The Redirect directive is prefix matching and everything after the match is copied onto the end of the target URL. So, in the case of the above rule, not-working-url (after the initial / that matches) is copied onto the end of https://www.example.com. With the Redirect directive, the trailing slash should nearly always match on the source and target URLs, otherwise you'll likely get missing slashes or double slashes in the redirect response.


RewriteCond %{QUERY_STRING} .
RewriteRule ^en/(.*) /$1 [L,NE,R=301]

Redirect 301 /en/working-url https://www.example.com/working-url
Redirect 301 /en/not-working-url https://www.example.com/not-working-url

Without further explanation, these rules don't make too much sense. The RewriteRule directive removes the /en prefix (when a query string is present), so the Redirect directives that follow would not match?

  • Related