Cloudflare rule: strip trailing slash - cloudflare

I'm able to remove trailing slashes by having two redirect rules:
No query string: https://foo.com/*/ -> https://foo.com/$1
Yes query string: https://foo.com/*/?* -> https://foo.com/$1?$2
But I'd prefer to do this with one rule.
I tried:
No/Yes query string: https://foo.com/*/\??* -> https://foo.com/$1$2
But I can't seem to get it working.
Any ideas on something else to try?

Related

.htaccess rule match part of new url and use as query string for old url

I am trying to match part of a URL and then use the matched expression to append onto the end of a query string from the old URL.
I have the following line in .htaccess, once I've worked out what I'm doing wrong I'll be able to fix the rest so for now I will just focus on the following line:
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1
I would like ([^/]*)$ to appear where $1 is
So essentially: /league/29/matches/ would point to index.php?page=1074&league=29
Can anyone please tell me what I am doing wrong? :)
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1
The $ (end-of-string anchor) after the subpattern ([^/]*)$, in the middle of the regex, does not make sense here and will cause the regex to fail. You should also be using the + quantifier here (1 or more). You are also missing the end-of-string anchor ($) at the end of the regex (otherwise the trailing /? is superfluous). Although you shouldn't really make the trailing slash optional on the rewrite as it potentially opens you up for duplicate content. (You should redirect to append/remove the trailing slash to canonicalise the URL instead.) You are also missing the L flag on the RewriteRule.
Try the following instead:
RewriteRule ^league/([^/]+)/matches/?$ index.php?page_id=1074&league=$1 [L]
Although, if you are expecting digits only (as in your example) then you should be matching digits, not anything. So, the following is perhaps "more" correct:
RewriteRule ^league/(\d+)/matches/$ index.php?page_id=1074&league=$1 [L]

rewrite spaces to redirect to underscores when following a specific domain url string and only

how to write a modrewrite that will replace a space %20 with an underscore that will look like this. http://cityinsider.com/b/ocean%20shores_wa/mikes-seafood-ocean-shores will redirect to http://cityinsider.com/b/ocean_shores_wa/mikes-seafood-ocean-shores . All the underscores are permanent, but sometimes there is a space between two and three word cities. so any spaces found after only this type and part of url: cityinsider.com/b/ need to be replaced . So this won't effect any other url strings that don't look like the above; for example it should not work on spaces that are immediately after domain root e.g. cityinsider.com/%20/appleWood and shoulnd't work where it doesn't follow the cityinsider.com/b/ such as cityinsider.com/c/%20
Try:
RewriteEngine On
RewriteRule ^([^/]*)\ ([^/]*)/(.*)$ /$1_$2/$3 [L,R=301]

Apache Rewrite Rule With % characters

I have a url that needs to be rewritten.
http://aplus-carpetcleaning.com/home.php%3Fcity%3DGainesville%2520VA
I have tried this:
^home\.php%3Fcity%3DGainesville%2520VA$ "http://aplus-carpetcleaning.com/home.php?city=Gainevilles%20VA"
This is not being recognized. Tried various alternates but no dice.
Notes:
%3F is '?'
%3D is '=',
%2520 is who knows?
The rewriting module applies your regex patterns to decoded urls, not the urlencoded string you see inside the browsers url field.
So create your regex pattern using unescaped characters instead. Something like:
^home.php\?city=Gainesville&20VA$
Fixed it with:
RewriteRule ^home\.php(.*)city(.*)Gainesville(.*)VA$ "http:\/\/aplus\-carpetcleaning\.com\/home\.php?city=Gainesville VA" [R=301,L]

.htaccess directory-to-file redirect not working with forward slashes

I need a basic redirect in a .htacess file, from a directory URL to a file:
RewriteRule ^(.*)/mydirectory/(.*)$ myfile.php?q=$2
It is not working. The problem is I can't get it to work unless I do:
RewriteRule (.*)mydirectory(.*)$ myfile.php?q=$2
in which case the captured string ($2) is "mydirectory" (?!), and of course the rule would apply to any URL containing "mydirectory" as a substring, even if it's not between slashes, which is no good.
The test URL is:
http://localhost/base/mydirectory/some-other-string
What I expect is:
http://localhost/base/myfile.php?q=some-other-string"
What I get with the first rule is nothing (rule does not apply/not working), and "myfile.php?q=base/mydirectory" with the second rule, respectively.
The same happens if I use RewriteCond: as soon as I add the damned forward slashes, the rule does not apply anymore. I even escaped the forward slashes, although there's no need for that, to no avail.
I'm not a mod_rewrite expert, but I'm no stranger to it. IMO, the first rule should work, but it doesn't. I'm probably doing something horribly wrong, but I'm too close to see it.
Please take a look, it's driving me nuts.
Many thanks.
Slashes have special meanings in regular expressions you need to escape them:
RewriteRule ^(.*)\/mydirectory\/(.*)$ myfile.php?q=$2

mod_rewrite replace all slashes (/)

I'm having troubles with the flag Next [N] in mod_rewrite
I wanna replace ALL slashes (/) in the url by an underscore. I've tried many regexps that work well for any other language, but to mod_rewrite doesn't.
Like:
/ _ [N]
or
(.*)/(.*) $1_$2 [N]
But the mod_rewrite seems enter in an endless loop.
I want a routine that works for X number of slashes, not limited to 3 or 4 slashes, for example.
Thanks.
The solution should be very similar to solution provided in
mod_rewrite: replace underscores with dashes
You essentially need two rules total, the first being the one using the Next [N] flag but with the addition of a check to ensure there are always 2 slashes (which you are missing). Then you need a final rule to finish it off with the redirect and include the last [L] flag.
Hope that helps