Redirecting all traffic based on Host name - apache

An external server (I'll call it "sub.origin.edu") redirects all traffic to my webpage. I want to take all traffic from this host, and redirect it to a different site (which I'll call "http://foo.target.edu/board/").
My .htaccess file is:
RewriteEngine On
RewriteCond ${HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board/ [R=302]
This doesn't seem to be working. I've confirmed (using PHP) that the host is indeed sub.origin.edu, and the .htaccess file is in the right directory, but this rule just doesn't come into effect. Any suggestions? Thanks.
(If I remove the RewriteCond, the redirect happens, so I can confirm that everything but the rewrite condition is working.)

Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board$1 [R=302]
You used the wrong substition character ($ instead of %)

I found this question while trying to complete a re-derict for specific hostnames.
This link was of great help to understand how RewriteCond and RewriteRule work.
http://httpd.apache.org/docs/2.4/rewrite/intro.html

If sub.origin.edu is doing a 3xx redirect, then the browser will issue a new request to your server using your.server.edu as the host name. So this rule will never match that. If this is the case, there's no easy way to tell where the request was redirected from.
If they're using a CNAME, Femi has the correct answer.

Related

htaccess Rewrite rule from "example.com/de/foo" to "example.de/foo"

RewriteRules can be such a pain for me, I cannot get this one to work.
I have to redirect urls like example.com/de/anypath to example.de/anypath.
[anypath] can be really any path, as I have to get it work for
example.com/de/articles/programming/hello-world (would be redirected to example.de/articles/programming/hello-world)
as well as for example.com/de/events/pic-nic (would be redirected to example.de/events/pic-nic).
This is what I wrote so far :
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
I also tried with RewriteCond with no more luck
RewriteCond %{HTTP_HOST} http://example.com/de
I am working with xampp, but tested on my web server with same
result.
I know this .htaccess file is working (get error if I enter a
typo)
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Any help would be appreciated !
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. ie. It should be "^de/(.*)$", not "^/de/(.*)$".
You don't need the double quotes and the NC flag is probably redundant, unless you also need to match dE, Ed or DE.
For example (near the top of the root .htaccess file):
RewriteRule ^de/(.*) http://example.de/$1 [R=301,L]
(HTTP, not HTTPS?!)
The trailing $ on the RewriteRule pattern was also redundant.
Test first with 302 (temp) redirect to avoid potential caching issues.
RewriteCond %{HTTP_HOST} http://example.com/de
The Host HTTP request header (ie. the value of the HTTP_HOST server variable) contains the hostname only. eg. example.com only in your example.
Any server variable that is prefixed with HTTP_ refers to the HTTP request header of the same name.
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Careful with testing 301s since they are cached persistently by the browser. You will need to clear your browser cache before testing!
I added two conditions (the first is to apply according to local or live site, the second to leave the node paths unchanged) :
RewriteCond %{HTTP_HOST} local.example.com
RewriteCond %{REQUEST_URI} !^.*/de/node/.*$
And it is working as expected. Thanks again.

htaccess - passing request uri as a parameter

What I'm trying to do is as follows:
Redirect user to another address using htaccess.
Pass the given URL as a parameter to the new url
So when the user visits any page on http://domainA.com/
he should be redirected to: http://domainB.com/?referer=http://domainA.com/
What I keep failing on, is retrieving the full URL the user came from BEFORE the redirection.
One of the things I tried and failed miserably:
RewriteRule ^(.*)$ http://domainB.com/?referer=%{HTTP_HOST}%{REQUEST_URI}
Thanks in advance!
Edit:
I tried to keep my question simple, as more complex questions tend to remain unanswered,
So I will sharpen my question, although there's a good answer to the original one.
So to be more specific about what I'm trying to achieve:
Both domains are hosted on the same host, and points to exact same files.
So domainA.com/file1.html and domainB.com/file1.html will display the same file.
What I want domainA to do, is to deliver all requests to a file called listener.php.
So that all requests to domainA should be like so:
User enters http://domainA.com/file1.html
Server request behind the scenes is actually: /listener.php?actualRequest=http://domainA.com/file1.html
I want this functionality to be on the server side so that the url will remain normal.
I went for 2 domains as I wanted to avoid redirect loops.
You can use this rule:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^ http://domainB.com/?referer=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
This handles both http:// and https:// URLs.
EDIT: As per edited question you can use:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule !^listener\.php$ /listener.php?actualRequest=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,QSA]

Why does mod_rewrite strip the port number from my request url?

I'm using Apache mod_proxy with mod_rewrite to mask some credentials in the query string. The logs show that the rewrite rule is working properly, but the port number (:9000) is being stripped causing the proxy to reject the URL. Aside from security lectures and anything else irrelevant to the actual question at hand, can anyone tell me what the solution might be?
Basic example using:
RewriteCond %{QUERY_STRING} ^(.*)userid=fakepass(.*)
RewriteRule ^(.*)$ $1?%1userid=realpass%2
Original URL: https://domain.com:9000/somedirectory/request.jsp?userid=theuser&password=fakepass
Intended URL:: https://domain.com:9000/somedirectory/request.jsp?userid=theuser&password=realpass
Actual result after mod_rewrite: https://domain.com/somedirectory/request.jsp?userid=theuser&password=realpass
Specify the port in your rule. Change your .htaccess to something like:
RewriteCond %{QUERY_STRING} ^(.*)userid=fakepass(.*)
RewriteRule ^(.*)$ https://%{HTTP_HOST}:9000/somedirectory/request.jsp?userid=theuser&password=realpass

Apache rewrite rule that ignores query/parameters, always redirects based on path

I'm trying to create a rewrite rule that will ignore any additional URL query/parameters and just redirect based on the path.
My company has a Wifi Hotspot service that does some DNS routing trick to force people to login before they can use it. Unfortunately when folks get disconnected from the WiFi and dropped back to their normal cell data service sometimes a URL request is still sent to our host, and it shows up as:
www.ourwebsite.com/login?dst=http://www.google.com/m?client=ms-android-verizon&source=android-home
I already wrote a set of rules to take care of base paths of /login and /login/ to redirect to our homepage,
RewriteCond %{THE_REQUEST} ^.*\/login/\ HTTP/
RewriteRule ^(.*)login/?$ "/$1" [R=301,L]
RewriteCond %{THE_REQUEST} ^.*\/login\ HTTP/
RewriteRule ^(.*)login?$ "/$1" [R=301,L]
but I am having trouble coming up with an appropriate string to ALWAYS redirect based souly on the path, and ignore any query parameters that may or may not come after.
Any help would be appreciate! Thanks in advance.
If I understood right, something like this should do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^login /? [R=301,L]
This rule-set will redirect to root as long as the incoming URL is something like:
http://www.ourwebsite.com/login?any_query
From Apache 2.4.0 on you can apply the QSD-flag to the rule.
When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.
-- https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsd
When using this flag for earlier Apache versions you'll cause an 500 Internal Server Error.

Force SSL redirect - Apache/IHS RewriteCond and RewriteRule

I'm trying to set up a rewrite rule which will force all requests coming in on port 80 to use HTTPS by force.
I'm only getting my head around mod_rewrite but this is what i currently have;
RewriteCond ${lowercase:%{REQUEST_URI}} /securePath$
RewriteRule ^(.*)$ https://www.mydomain.com/$1
In the RewriteCond securePath is the requested path (not including my domain).The full URI would be www.mydoamin.com/securePath
In the ReWriteRule $1 is supposed to be the output from ${lowercase:%{REQUEST_URI}} in the RewriteCond
However when i restart my IHS server and attempt to hit the URL it isint forcing access through HTTPS. Any suggestions on what is wrong with these two lines?
Thanks
RewriteRule ^securePath/(.*)$ https://www.mydomain.com/$1 [NC,R=301,L]
Probably it's just you example being broken, but you are explicitly lowercasing the incoming request path and try to match that to "/securePath" which includes a capital letter. -That is never going to match.