Reputation: 552
I broke my head trying to find where the issue is:
I have the following redirection rule:
RewriteRule ^/productname(.*) https://website.com/category [R=301,NC,L]
but it doesn't work and I can't get why. Because this rule:
Redirect 301 ^/productname(.*) https://website.com/category/subcategory/productname
works fine.
Would appreciate any help
Upvotes: 1
Views: 27
Reputation: 45914
RewriteRule ^/productname(.*) https://website.com/category [R=301,NC,L]
That won't work in per-directory .htaccess
files because the URL-path matched by the RewriteRule
pattern is less the directory-prefix (the filesystem path of where the .htaccess
file resides). The directory-prefix always ends in a slash, so the URL-path that is matched by the RewriteRule
pattern never starts with a slash.
From the Apache docs for the RewriteRule
directive:
In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.
The directory path where the rule is defined is stripped from the currently mapped filesystem path before comparison (up to and including a trailing slash). The net result of this per-directory prefix stripping is that rules in this context only match against the portion of the currently mapped filesystem path "below" where the rule is defined.
So, you would need to do remove the slash prefix, for example:
RewriteRule ^productname https://website.com/category [R=301,NC,L]
The trailing (.*)
on the RewriteRule
pattern is superfluous in this example.
Redirect 301 ^/productname(.*) https://website.com/category/subcategory/productname
This rule wouldn't "work fine". I think you mean RedirectMatch
.
Note that RewriteRule
and Redirect
(and RedirectMatch
) belong to different modules. mod_rewrite and mod_alias - you should avoid mixing redirects from both modules as you can get unexpected conflicts.
Upvotes: 1