Rewrite address bar without redirecting - apache

I have a host in my university's http server under the domain: university.com/~username
Now, I also have a domain on: mydomain.net with GoDaddy
I want to know if it is possible to have a subdomain:
university.mydomain.net that basically redirects to
university.com/~username.
Now, the trick here is that if I want to access
university.com/~username/subdir via university.mydomain.net/subdir
the address bar in the browser shows: university.mydomain.net/subdir no
matter if I access it via university.com/~username or university.mydomain.net
The problem with using permanent redirect with masking in GoDaddy, then if
I am in the dir university.mydomain.net/subdir and go to the subsubdir
university.mydomain.net/subdir/subsubdir then the browser still shows university.mydomain.net/subdir
And about CNAMEs or other kind of register in GoDaddy, I really have no idea
what to do (this is my first domain) and I don't even know if I have the rights
in my university server to make a difference.
I tried with a RewriteRule in a .htaccess file, but this always tries to
redirect (no matter what flag I use) to university.mydomain.com/subdir
while the only thing I want to do is that the address bar shows the direction
without following it.
I really don't even know if this is possible somehow. Sorry if it's an stupid
question, I'm very new with domains and those stuffs.

Under the CNAME you need to add you subdomain entry (in the Godaddy DNS management console)
university.mydomain.net points to #
and in your host the # might be pointing to the IP address where your university server is running.
Then in your apache vhost config, you would need to have rewrite rule like
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^university.mydomain.net(:80)?$
RewriteRule ^/(.*) http://university.com/~username/$1 [P,QSA,L]
RewriteCond %{HTTP_HOST} ^university.com(:80)?$
RewriteRule ^/~username/(.*) http://university.mydomain.net/$1 [R=301,L]
</VirtualHost>

Related

Redirect non-www to www https Apache XAMPP

Please forgive what may sound like a similar question to what has been asked, however, I am VERY new to XAMPP and Apache and I have tried all possible combinations of the Rewrite Rules mentioned in other threads, which I have placed in the httpd.conf, httpd-default.conf and also in .htaccess and I simply cannot get the rewrite rules to work.
I simply want to redirect example.com to www.example.com. Please note that my redirect from HTTP to HTTPS is working 100%, so if someone can please advise on exactly where I should place the rewrite rule, and which one to use, to force non-www to www, I would be most appreciative.
I have full access to the server so I can edit either .conf or .htaccess files. Also I have checked and the part in httpd.conf that allows overrides to read the .htaccess files is enabled. The .htaccess file is currently in the same folder where my website lies. Just to add, I don't get any error, it just says the page has timed out if I type in example.com, if I type www.example.com then the page loads as expected.
Try this in your .htaccess file:
Replace example.com with your url.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
The connection has timed out
This is a DNS or server config error. The request is not even reaching your server, or your server is not responding to the request. (So any attempt to implement a redirect will naturally fail.)
A quick check of the DNS for example.com and www.example.com shows they are pointing to different IP addresses (you have an A record configured for each). The example.com host is 1 digit different and would appear to be incorrect (the IP address does not respond to requests, hence the "time out").
Instead of having a separate A record for each hostname. If the www subdomain is supposed to point to the same site as the domain apex (ie. example.com) then configure a CNAME record instead that points www.example.com to example.com (this is not a "redirect").
You then need to ensure that your server will accept requests to the non-www hostname. For example:
ServerName example.com
ServerAlias www.example.com
(It's possible you have the ServerName and ServerAlias reversed - which is "OK".)
And the SSL cert needs to cover the non-www hostname (it does).
You can then implement your non-www to www HTTP redirect as mentioned.

Apache redirect after a rewrite

I'm trying to figure out how to properly do this. I'm hosting a domain that used to have a website also on the same server, however the website has now been moved to a different machine, but they want to keep the domain hosted on our DNS. Rather than changing the DNS record right now, I'm trying to figure out how to do a proxy redirect but I'm having some trouble.
Right now, I'm using the RewriteEngine to rewrite the URL as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.org [NC]
RewriteRule ^/(.*) http://www.domain.org/$1 [L,R]
This is in case someone looks up the website simply by http://domain.org it will get rewritten to http://www.domain.com - that works fine.
Now I need to redirect it to go to an IP address with a username:
http://111.222.333.444/~user
Rather than simply redirecting it to that address, I'd like to do a proxy where the domain will still be visible in the browser's address bar, while also keeping the above rule in place.
Suggestions anyone?
Make sure mod_proxy is enabled and do:
<VirtualHost *:80>
ServerName www.domain.com
ProxyPass / http://111.222.333.444/~user
</VirtualHost>

Subdomain forward to another subdomain without redirecting

I've spent an amout of time to try solve it, and finally without any effects.
I have a casual subdomain like test.example.com and a mobile subdomain m.test.example.com. Every subdomain has own directory test (I have my scripts here) and m.test.
I need to forward users from m.test.example.com to test.example.com without redirecting to test.example.com.
I know it easy to do it by Virtual Hosts or cpanel, but I need to do it on shared hosting with DirectAdmin (user level only). I think about mod_rewrite, and I also tried PHP symlink(), but it is disabled on my hosting.
How can I forward the user from the mobile subdomain to non-mobile resources?
Since the two domains ("test" and "m.test") have different document roots, you're going to need to reverse proxy. The problem here is mod_proxy, the proxy module, may not be loaded by your host, but you'd do something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.test\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/$1 [L,P]
These rules would be in the /m.test/ folder, the document root of the m.test.example.com subdomain. The P flag tells mod_rewrite to hand the request to mod_proxy to reverse proxy instead of redirecting the browser.

Apache mod rewrite to direct from one server to another

I've got to apache web servers running. One on port 80 and another on port 8077.
I'm wanting to try and redirect all the traffic through the port 80 version if possible.
What I'm ideally wanting is to be able to go to http://translate.example.com
and the traffic get directed to http://www.example.com:8077
I've already got a lot of mod_rewrite going on at the main port 80 server, but I'm not sure which of the servers needs configuration or whether both do.
I'm wanting to make sure that translate.example.com/img (or any other subdirectory) actually points to the 8087/images directory.
update
I've now got the following:
RewriteCond %{HTTP_HOST} example[NC]
RewriteCond %{REQUEST_URI} ^/glot$ [NC]
RewriteRule ^(.*)$ http://www.example.com:8077/$1 [P]
ProxyPassReverse / http://www.example.com/
I'm getting to see the other servers new pages, but I'm finding all the resources aren't found like images, css etc
Doing a view source all the resources in the installed product are set with leading slash
For example
/img/glotpress-logo.png
So I'm not sure how to get the resources loaded up.
Note I'm happy enough if the original starting point is www.example.com/glot instead of glot.example.com as in the original question
You can remap your resources to another server but having the other server on non-default port might prevent some of your visitors from viewing them as they might be block (firewalled) from accessing the port. If you're not worry about the port being block you can use mod_rewrite Remapping Resources. method.
RewriteEngine On
RewriteRule ^/images/(.+) http://www.example.com:8077/images/$1 [R,L]
If you want to make sure that everyone is able to view the external resources, you need to use proxy where apache will tunnel the visitor connection to example.com:8077 transparently. You can read more about mod_rewrite for Proxying at apache website.
RewriteEngine on
RewriteBase /images/
RewriteRule ^images/(.*)$ http://example.com:8077/images/$1 [P]
ProxyPassReverse /images/ http://example.com/images/
UPDATE
Have you tried to remove
RewriteCond %{HTTP_HOST} example[NC]
This line basically tells that it will only process if the HTTP_POST is example.com if its coming from www.example.com this rule is not applicable. I'm hoping that "example[NC]" is a typo.
In the end it probably looks like
RewriteRule ^/glot/(.*)$ http://www.example.com:8077/glot/$1 [P]
ProxyPassReverse /glot/ http://www.example.com:8077/glot/
You need to do the configuration at the port 80 server to make it act as a proxy for the 8077 server.
The Apache document is here: http://httpd.apache.org/docs/trunk/rewrite/proxy.html

Apache mod_rewrite ANY subdomain(s) to root domain, unless existent as virtualdocumentroot

Say you've got an Apache2 virtual host setup, something like this:
/htdocs/parent1.com
/htdocs/sub1.parent1.com
/htdocs/sub2.parent1.com
/htdocs/parent2.net
/htdocs/parentn.org
Say you'd like to do this with VirtualDocumentRoot /htdocs/%0, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None.
Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?
I may have solved my own problem. However I would appreciate any feedback if somebody finds a problem with what I'm doing.
The following leaves alone any request to an arbitrary subdomain, as long as there exists a corresponding document root; but redirects any request to a subdomain which does not exist in the filesystem.
<IfModule rewrite_module>
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d
RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$"
RewriteRule ^/(.*)$ "http://%1/$1" [R=301]
</IfModule>
Allows me to setup wildcard DNS and use name-based virtual hosting, without touching any configuration settings. Also, there's no htaccess involved. Just make your folder with any name like "/htdocs/[host.]domain.tld" and you're up and running. As far as I can tell, this doesn't really work with SSL/TLS (presumably something to do with %{HTTP_HOST}?), but secure sites are comparably few and better resolved by IP address than by hostname.