rewrite rule base on first sub domain to directory - apache

I need to redirect the first sub domain of any domain name that will hit the root folder of that web server to a subfolder.
It need to keep the incoming port (stupid isp that block port 80 and 443).
It need to be able to handle both http and https.
It need to ignore www.whateverthedomain.ext
Ex.
dom1 : mydummy.com
dom2 : toberedirected.net
dom3 : putanydomainhere.ca
possible incoming url
http(s)://firstsub.mydummy.com:8082
http(s)://whateveryoutypehere.thegoodsub.toberedirected.net
http(s)://firstsub.mydummy.com/firstsub/
http(s)://www.mydummy.com/
The result of the 3 incoming url should be
http(s)://firstsub.mydummy.com:8082/firstsub/
http(s)://thegoodsub.toberedirected.net/thegoodsub/ (i dont care what is in front of the first sub)
http(s)://firstsub.mydummy.com/firstsub/ (dont rewrite, it is not the root folder)
http(s)://www.mydummy.com/ (dont rewrite, ignore www and give default webpage of the domain)
It is probably easy to write but i cannot figure it out this morning.

Try adding this to the htaccess file in your document root
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.
RewriteRule ^/?$ /%1/ [L,R=301]
This should preserve the hostname and port when redirecting. It will only redirect requests for the web root, and will redirect to the name of the subdomain, with a trailing /.

Related

Apache redirect for adding missing .html at the end

I want to redirect any request that doesn't have the last segment contain a dot to .html
examples:
/travels/
/travels.html
/travels/categories/new-zealand
/travels/categories/main.js
Line 2 and 4 should not be redirected while line 1 and 3 should be redirected to:
/travels.html
/travels/categories/new-zealand.html
I already have this regex that seems to work for capturing: ^(https?:\/\/.+(\..+)*\/)?[^\s.]+$
How do I exactly make this redirect happen? I have a vserver with an apache werbserver that is running and mod_rewrite is enabled. How exactly would the rewrite statement look and where do I put it? If I put it in a .htaccess file, where do I keep that? Inside the root of the page or anywhere?
You can not match against host and https header in the rule's pattern. You can only match against URL path in a RewriteRule . To check the host and https header you need to use RewriteConds
RewriteEngine on
# URL scheme is HTTPS
RewriteCond %{HTTPS} on
# Host is www.example.com or example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
#URL path is /travels/ then redirect the request to /travels.html
RewriteRule ^/?travels/$ /travels.html [R,L]
This will redirect https://example.com/travels/
to https://example.com/travels.html . This changes the URL in your browser from old to the new one, if you want the redirection to happen internally then just remove the R flag.

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

.htaccess Redirect and proxy ports

I have an apache server that is part of a cloud servers network.
The virtual server its configured to respond at port 9000, but you access from outside through port 80. Thats working fine, except for when I try to make a redirect from the .htaccess file
I have the next 2 lines to redirect errors to a folder index
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule .* /folder/ [R=301,L]
That tries to redirect the user to:
www.domain.com:9000/folder/, and it should be redirecting to www.domain.com/folder (which means, www.domain.com:80)
How can I avoid that?
thanks!
I solved it changing the rewrite rule to:
RewriteRule .* http://%{HTTP_HOST}/folder/ [R=301,L]

htaccess redirect domain and mask url

I want to redirect a domain name : http://www.newdomain.com to http://sub.maindomain.com
I have edited .htaccess as:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !sub.maindomain.com$ [NC]
RewriteRule ^(.*)$ http://sub.maindomain.com/$1 [L,R=301]
It redirects but the http://sub.maindomain.com visible in the address bar . Help! How to do the masking so that http://www.newdomain.com would be visible . Thanks
And also I want create a subsite http://sub.newdomain.com . How to do it with .htaccess .
If www.newdomain.com and sub.maindomain.com are on different hosts, the only way to get the content on sub.maindomain.com to be served while the browser's URL address bar says www.newdomain.com is to fetch the content from sub.maindomain.com on behalf of the browser. Since as far as the browser is concerned, it's looking at content on newdomain.
You can do this either by setting up a proxy using apache or maybe a script. Using apache, you'd need access to your vhost config for the www.newdomain.com domain. You can set it up like:
ProxyPassMatch ^(.*)$ http://sub.maindomain.com$1
Take a look at the mod_proxy docs for more info on the flags you can pass this directive, to tweak timeouts and retries.
If you want to setup a php script to proxy, you'll need to route all requests to this php script, so in your htaccess file in your www.newdomain.com document root, add (assuming this php script is called proxy.php):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/proxy.php
RewriteRule ^(.*)$ /proxy.php?url=$1 [L,QSA]
Then in proxy.php, look for the url query string, load that page, and output it to the browser.
Though all of this is much easier if you can just point both domains to the same host.