How to urlencode (escape) strings in htaccess? - regex

I have this case in htaccess
RewriteRule ^.*$ https://example.com/ [CO=ref:%{HTTP_REFERER}:example.com:0:/]
All I need is to escape string %{HTTP_REFERER} but after half an hour googling and reading apache docs it seems I can't solve this one :)
So, how do I escape strings in apache?

If you want escaping behavior you will need to add this line in your Apache config file:
RewriteMap escape int:escape
Then restart the Apache server.
Further you need to modify your rewrite rule like this:
RewriteRule ^ https://example.com/ [CO=ref:${escape:%{HTTP_REFERER}}:example.com:0:/]

Related

How can I redirect users from a url to another using regex and htaccess?

I want to redirect users from www.example.com/ANYTHING to www.example.com.
Is .htaccess the better way to do it? How can I do it?
If you really just have to remove everything after www.mydomain.com, then you just have to delete everything from the first / to the end of the URL:
Search for this regex
%^([^/]*).*$
and substitute with \1, as I did here. Note that I have used the % sign as delimiter instead of /, so I don't need to escape the / in the regex. (I could have used any other available symbol other than /.)
You'll need to use a mod_rewrite RewriteRule (as opposed to a mod-alias RedirectMatch) in order to avoid conflicts with mod_dir and the DirectoryIndex*1.
For example, in .htaccess (Apache 2.2 and 2.4):
RewriteEngine On
RewriteRule . / [R,L]
The single dot matches something (ie. not the document root) and we redirect to the document root.
However, if the document root is an HTML webpage that links to resources like JavaScript, CSS and images then you need to make exceptions for these resources, otherwise these too will be redirected to the root!
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png|gif)$ [NC]
RewriteRule . / [R,L]
*1 A mod_alias RedirectMatch directive such as RedirectMatch /. / ends up matching the rewritten request (by mod_dir) to the DirectoryIndex (eg. index.php) resulting in a redirect loop.

RewriteRule to remove superfluous single "?" in URL

I am using IBM HTTP server configuration file to rewrite a URL redirected from CDN.
For some reason the URL comes with a superfluous single question mark even when there are no any query string. For example:
/index.html?
I'm in the process of making the 301 redirect for this. I want to remove the single "?" from the url but keep it if there is any query string.
Here's what I tried but it doesn't work:
RewriteRule ^/index.html? http://localhost/index.html [L,R=301]
update:
I tried this rule with correct regular expression but it never be triggered either.
RewriteRule ^/index.html\?$ http://localhost/index.html [L,R=301]
I tried to write another rule to rewrite "index.html" to "test.html" and I input "index.html?" in browser, it redirected me to "test.html?" but not "index.html".
You need to use a trick since RewriteRule implicitly matches against just the path component of the URL. The trick is looking at the unparsed original request line:
RewriteEngine ON
# literal ? followed by un-encoded space.
RewriteCond %{THE_REQUEST} "\? "
# Ironically the ? here means drop any query string.
RewriteRule ^/index.html /index.html? [R=301]
Question-mark is a Regular Expression special character, which means "the preceding character is optional". Your rule is actually matching index.htm or index.html.
Instead, try putting the question-mark in a "character class". This seems to be working for me:
RewriteRule ^/index.html[?]$ http://localhost/index.html [L,R=301]
($ to signify end-of-string, like ^ signifies start-of-string)
See http://publib.boulder.ibm.com/httpserv/manual60/mod/mod_rewrite.html (for your version of Apache, which is not the latest)
Note from our earlier attempts, escaping the question-mark doesn't seem to work.
Also, I'd push the CDN on why that question-mark is being sent. This doesn't seem a normal pattern.

Redirect using .htaccess to remove a part of filename in Apache

I know there are plenty of questions how to replace part of URL with something else (or nothing) using .htaccess but I really suck both in regular expressions and in .htaccess.
How can I to convert URL like /v0/A8B9DEBF512F929144257AEE00262C16/$File/IMG_8819.jpg to /v0/A8B9DEBF512F929144257AEE00262C16/IMG_8819.jpg? (Without $File/, instead of A8B9DEBF512F929144257AEE00262C16 and IMG_8819.jpg there may be any combination of digits and letters).
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(v0/[^/]+)/\$File/(.*)$ /$1/$2 [L,NC,R=301]

Apache LocationMatch regex ignores duplicate slashes

I'm having a strange behaviour with Apache's LocationMatch directive when there are extra slashes at the beginning of the URL. According to the Apache docs if I'm reading it right this should work:
<LocationMatch ^/appcontext/(a|b)>
SetHandler weblogic-handler
WebLogicCluster apphost01:xxxx,apphost02:xxxx
WLProxySSL ON
</LocationMatch>
However if I type the following URL it is also being forwarded to the backend hosts:
https:// <hostname:port> ////////appcontext/a/
In the Apache docs it clearly states that it should apply the directive only for /appcontext/a/, unless I'm missing something with the regex or there is some issue with the mod_wl plug-in
http://httpd.apache.org/docs/2.2/mod/core.html
"For example, <LocationMatch ^/abc> would match the request URL /abc but not the request URL //abc"
Does anyone have any suggestion to achieve this?
Yes Apache does collapse extra slashes in rewrite rule. You can use THE_REQUEST variable to match and remove extra slashes. Place this in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)//+(.*)$
RewriteRule ^ %1/%2 [R=302,L,NE]

mod_rewrite RewriteRule anomaly

I'm preparing rewrite rules for my site. And I'm faced problem when target URL contains characters like "%3A".
Apache mod_rewrite just removes "%3" when rewrites url.
For example I need rewrite url
/primed-white-mdf-skirting+architrave/
to
/Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=%3AtopSellers%3AColour%3AWhite&text=#
I have generated rule for this. Here it is:
RewriteRule ^primed-white-mdf-skirting\+architrave/ /Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=%3AtopSellers%3AColour%3AWhite&text=# [R=301,L,NE]
So rewrite pass to:
/Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=AtopSellersAColourAWhite&text=%23
Why this happens? Please help
You need to escape the % otherwise %3 is considered a back-reference of captured group from RewriteCond:
RewriteRule ^primed-white-mdf-skirting\+architrave/ /Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=\%3AtopSellers\%3AColour\%3AWhite&text=# [R=301,L,NE,QSA,NC]