RewriteCond clarification - looking for how to implement RewriteRule Condition Pattern in bulk
Platform: Apache .htaccess file
Reference Link: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
Case: I have a series of pages that I want all of them routed to a single page, but only what matches the specified condition pattern should land there (e.g. passing the filter).
Typically, I handle un-suffixed URIs to route to the corresponding hosted file for execution in this way:
This will only match /home and not /sub/home. ✅
In this case, there is a sizeable list of URI's I'd like to all route to a specific page at this stage of the build using the RewriteCond statement:
This works except it treats www.mysite.com/sub/apples the same as www.mysite.com/apples.
When I try to add ^ in front of the CondPattern, it doesn't match anything and no rewrite happens.
#doesn't work
How can I construct a CondPattern in RewriteCond that's equivalent to a CondPattern in RewriteRule that has ^ in the filter expression?
5 Replies
i don't understand what you need
you want to take
/apples/
and redirect to somewhere else?This should work:
I think RewriteCond includes the root
/
from the URL, where RewriteRule
seems to notrewrite rules don't include the start slash
but this is super un-necessary
this is the exact same thing:
my example only included a short list of URIs for the case of the question. There is a long list so I was inclined to put them all in RewriteCond statements followed by a single rule statement rather than 1 "heavy" rule.
For any others reading this question: this site has been helpful to test-run rewrite scenarios: https://htaccess.madewithlove.com/
If I'm following what you're saying, the RewriteCond needs the
/
included in the expression to work, whereas the RewriteRule does not need the leading /
in the expression for it to work. 👍that site has helped me in the last 8-10 years