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:
RewriteRule ^home(/?)$ /pages/home.php [L]
RewriteRule ^home(/?)$ /pages/home.php [L]
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:
RewriteCond %{REQUEST_URI} (apples|oranges|bananas)(/?)$ [NC]
RewriteRule .* /pages/fruitbasket.php [L]
RewriteCond %{REQUEST_URI} (apples|oranges|bananas)(/?)$ [NC]
RewriteRule .* /pages/fruitbasket.php [L]
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
RewriteCond %{REQUEST_URI} ^(apples|oranges|bananas)(/?)$ [NC]
RewriteRule .* /pages/fruitbasket.php [L]
RewriteCond %{REQUEST_URI} ^(apples|oranges|bananas)(/?)$ [NC]
RewriteRule .* /pages/fruitbasket.php [L]
How can I construct a CondPattern in RewriteCond that's equivalent to a CondPattern in RewriteRule that has ^ in the filter expression?
5 Replies
ἔρως
ἔρως5mo ago
i don't understand what you need you want to take /apples/ and redirect to somewhere else?
Jochem
Jochem5mo ago
This should work:
RewriteCond %{REQUEST_URI} ^/(apples|oranges|bananas)(/?)$ [NC]
RewriteRule .* /pages/fruitbasket.php [L]
RewriteCond %{REQUEST_URI} ^/(apples|oranges|bananas)(/?)$ [NC]
RewriteRule .* /pages/fruitbasket.php [L]
I think RewriteCond includes the root / from the URL, where RewriteRule seems to not
ἔρως
ἔρως5mo ago
rewrite rules don't include the start slash but this is super un-necessary this is the exact same thing:
RewriteRule ^/(apples|oranges|bananas)(/?)$ /pages/fruitbasket.php [NC,L]
RewriteRule ^/(apples|oranges|bananas)(/?)$ /pages/fruitbasket.php [NC,L]
ProdJuan
ProdJuanOP5mo ago
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. 👍
ἔρως
ἔρως5mo ago
that site has helped me in the last 8-10 years
Want results from more Discord servers?
Add your server