Caddy rewrite based on User-Agent not working - caddy

I'm trying to rewrite a path if the requested client is a mobile user. As per caddy documentation, this code should redirect a mobile user to the specified destination.
rewrite /redirect-me {
if {>User-Agent} has mobile
to /redirected
}
But it's wont when I add the User-Agent condition. I tried other condition which works just fine. I tried to look for caddy available directives like User-Agent but can't find a single hint.

I think it is likely {>User-Agent} is case sensitive.
try {>User-agent} and see if that works.

I recently ran into a similar issue and wanted to share a solution that worked for me on Caddy 2.6.1. I used a named matcher to match requests that are both from a mobile device and are directed to /redirect_me. Here is the relevant portion of the Caddyfile:
#mobile {
# Check if the string "mobile" is in the User-Agent portion of the request header.
header_regexp User-Agent (?i)(mobile)
# Only match the request if it is to the /redirect_me path.
path /redirect_me
}
# Rewrite the previous request to the desired path.
rewrite #mobile /redirected

Related

IIS dynamic URL rewrite

I'm trying in Microsoft IIS 10 to do dynamic URL rewrite / redirect based on a condition that the URL contains specific info.
Match URL / Pattern: mysite/pages/viewpage.action?uniqueurl
Rewrite URL: mysite.xyz.xyz/pages/viewpage.action?uniqueurl
What I want to achieve is that whatever you write after mysite/pages/viewpage.action? it needs to rewrite to the same URL just with mysite.xyz.xyz added instead of only mysite.
Example 1:
mysite/pages/viewpage.action?thisisexample1 -> mysite.xyz.xyz/pages/viewpage.action?thisisexample1
Example 2: mysite/pages/viewpage.action?anothersite -> mysite.xyz.xyz/pages/viewpage.action?anothersite
When I'm trying to set up the above in the URL rewrite section i just end up at: https://mysite.xyz.xyz/pages/viewpage.action.
I have a generel HTTP redirect aswell to https://mysite.xyz.xyz when you come from mysite which works fine but I need to try and get specific URL rewrites to work aswell.
I found the solution.
It was to add HTTP redirect parameters to the HTTP redirect and change the Redirect Behaviour.
I added $S$Q to the HTTP redirect which fixed the issue in the regards to the question mark in the redirect which IIS couldn't handle without the extra parameters.
Besides that I need to change the Redirect Behaviour so I put a checkmark in 'Redirect all requests to exact destination (instead of relative to destination)
Edit: Typo

IIS URL Rewrite for a folder to another web server

I have an IIS webserver running a website. I've installed ARR3 to enable routing rules.
I'd like to redirect all the incoming request for the URL .mydomain/url2redirect/ to an Apache server installed on the same machine where is running SVN.
So in the inbound rule definition I've inserted the following:
Input: URL path after '/'
Pattern: *.mydomain/url2redirect/*
Action Type: Rewrite
Action URL: https://ip-address:8080/{R:2}
By testing the pattern, I get the desired result, and in the R:2 back reference I get the requested page I need to pass to SVN, but I get always a 404.
"url2redirect" doesn't exist but it should captured by the URL rewrite and forwarded to the Action URL.
I've added any outbound rule. Should I have to add something? what?
I've tried also to add a Reverse Proxy Rule, using the ARR template but, it's completely ignored, therefore I think that the routing is not working at all on IIS.
As far as I know, the url rewrite rule Pattern doesn't match the domain part.
That means mydomain will not include in the Pattern.
If you want to match the url2redirect, you should use below pattern.
url2redirect(.*)
Then I suggest you could modify your action url as below:
https://ip-address:8080{R:1}

Apache .htaccess redirect to an anchor

I'm trying to do a one-off damage-limitation redirection to an anchor on a page on a website. A wrong URL got published in some publicity material, like this:
https://mydomain.org.uk/A/B
when what I really wanted to publish was
https://mydomain.org.uk/A#B
Having looked at some other answers it seems that any redirect with an anchor needs to be an absolute URL. So I put this in my .htaccess:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,L]
(note, the .php is correct, A.php is the page file). And it just simply doesn't work. The browser simply loads A.php and displays it from the top.
I know that the rule pattern is matching, because if I make the target be a completely nonexistent page I get a 404 as expected.
Unfortunately my web hosting service doesn't let me use the Apache log, so it's hard to trace what's going wrong. Can anyone guide me to how to do the rewrite properly so that I pass the #anchor all the way through to the user's browser?
Thanks in advance!
When the RewriteRule is processed by the server, it basically changes internally which resource to access, without the browser noticing.
The only way to change the URL in the browser is to use the redirect flag. This will make the webserver send a HTTP 302 response with a Location header, which then will result in the browser changing the URL and requesting the new page. This new URL can contain an anchor.
In your case the following rule should work:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,R,L]
Please keep in mind that anchors are a browser feature so they are normally not sent to the server and therefore neither appear in access logs nor can be used in a RewriteRule.

Magento Multi Store URL rewrite

We are working on a Magento site that has several different stores. We are working on rewriting the url so when you are in a specific store, the store name is added to the url. We have tried a couple of different things. First by turning on "Add Store Code to Urls" in System>Configuration>Web. This causes one of the store names to be added to the url twice and the rest of the stores the base store name is added. Our second idea was to add:
SetEnvIf Request_URI ^\/site_wow\/ MAGE_RUN_CODE=site_wow
SetEnvIf Request_URI ^\/site_ahepa\/ MAGE_RUN_CODE=site_ahepa
SetEnvIf Request_URI ^\/site_ita\/ MAGE_RUN_CODE=site_ita
SetEnvIf Request_URI ^\/site_deca\/ MAGE_RUN_CODE=site_deca
to the .htaccess file in the public_html folder on the site. This works for the ahepa store but none of the others. If I change the other stores to MAGE_RUN_CODE=site_ahepa, it adds the site_ahepa to the url.
I found a similar question here: Magento site switching .htaccess but am not having any luck fixing the problem. If there is anyone who could send me in the right direction I would greatly appreciate it.
Ok, here is what I got to work. I have the .htaccess file as it is above. In System>Configuration>Web, I have Add Store Code to Urls set to YES and Use Web Server Rewrites set to NO. The url rewrite for the ahepa store is different that the other stores but all of the links redirect to the correct stores and the products are showing up where they are supposed to so I am not going to mess with it anymore.

redirectmatch changes post to get

I have two separated vhost. On the www. one, I have instaled this only line on .htaccess
redirectMatch 301 ^(.*)$ http://d_blur_blur_s.com$1
All works as expected except that in the case where the POST is converted to GET.
Please notice that the post has parametres as a get (i didn't do it, and I won't change it) i am just trying to avoid duplicated content. I am showing firebug trace.
I expect to have the POST redirected on the main domain with redirectmatch or other trick.
Update
I have half of the internal links on the site written with www and the other half without it. So I need to keep both public but not duplicated. I need the GETs forwarded as GETs and the POSTs frowarded as POSTs. the problem is big, I have 12000 pages indexed, and lot of forms. So I am first searching for a generic solution without changing code. I full control de server.
Thanks a lot
A redirect response from your server for whatever request you made, regardless of it being POST or GET request, always results in your client making a GET request (unless you somehow enable it to NOT automatically follow redirects--sometimes I do this with curl, but I don't know any widely used browsers with this functionality available easily to the user). The client browser takes the URL provided by the redirect and treats it as a URL to perform a GET request. No way around it, the POST data is discarded by the server since it was intended for the server resource at the original URL.
If you redirect outside of .htaccess, say in a PHP file to construct redirect response, your only option is to convert the POST parameters into a string of GET parameters and add it to the end of the URL you send back to the client with your redirect response.
I'm fairly confident there is not a way to do automatic POST parameter appending to redirect in the .htaccess file or in the httpd.conf files whether you use the redirect directive or the rewrite directive via the mod_rewrite module.
You redirect using 307 instead of 301 to keep the post data, but some browsers show a dialog to the user if he is sure he want to sent the post data, so not real pretty.
But I'd rather go and fix the problem at the root. The only way you would get a post to the wrong domain is if the html-form refers to the wrong domain (e.g. <form action="www.d_blur_blur_s/public/main/loginGenerator.php" ...). Just fix the html.
Don't worry about duplicated content as search engines never send post-requests, only get. You current solution should do the trick to prevent duplicated content.
The only way to do a redirect and preserve POST data (that I am aware of) is to use mod_rewrite with mod_proxy and then use P flag in RewriteRule.
On your www host enable mod_rewrite, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^ http://d_blur_blur_s.com%{REQUEST_URI} [P]
I bumped into this POST->GET rewrite as well. An API call like this:
curl -X POST https://example.com/api/user/123456789
was entering my API framework with a GET request method.
The cause seems to be related to POST requests with an empty body. To avoid this issue you can set the Content-Length header:
curl -X POST https://example.com/api/user/123456789 -H 'Content-Length: 0'
or pass something in the body:
curl -X POST https://example.com/api/user/123456789 -d ''
In HTTP 1.1, statusCode (307) indicates that the request should be repeated using the same method and post data. Change the redirect type to fix it.
Adding www to the base URL did it for me.

Resources