Redirect wildcard subdomains to subdirectory, without changing URL in address bar - apache

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
Here's what I have so far:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...

First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
Now, if you open asd.domain.com it should redirect you to domain.com/asd.
You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p

This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]

I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:
/
var/
www/
html/
.htaccess
subdomain1.domain.com/
subdomain2.domain.com/
subdomain3.domain.com/
Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.
Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.

Related

Force to SSL maintaining the URL

I don't have much experience with VHOST and SSL, I tried lot of possibilities but none of them is working.
I have a website with 2 folders:
www.example.com/userpage
www.example.com/adminpage
If I go to www.example.com, my loginsystem automatically redirect to www.example.com/userpage.
If I want to go to the admin section, I have to write manually www.example.com/adminpage.
Now I switched to SSL, and everything is working if I type https://....
But I cannot understand how to force the redirect from http to https.
I wrote this in my apache vhost file:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond ${HTTPS} !=on
RewriteRule (.*) https://example.com/$1 [R=301,L]
</VirtualHost>
But it's not working.
How can I manage it?
If I write www.example.com/adminpage it have to redirect me to https://www.example.com/adminpage.
Actually it should work for every subfolder, if for example I send an email to a user saying "hey user, please check your account www.example.com/user/check_account.php?userid=14125114
it have to then redirect it to automatically:
https://www.example.com/user/check_account.php?userid=14125114
so it should work for every page and every subfolder.
Thank you for your suggestions
Should be quite simple. Replace this block:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond ${HTTPS} !=on
RewriteRule (.*) https://example.com/$1 [R=301,L]
With this:
Redirect permanent "/" "https://example.com/"

Using .htaccess to redirect all requests to subdomain

I'm hosting a Laravel application on my server and have set up a subdomain to host it in my virtual host.
I have another subdomain on my server and after hours of playing around trying to set up an .htaccess file, I came up with the below which redirects all requests to www.mysite.net/example to my subdomain example.mysite.net (e.g www.mysite.net/example/12345 goes to example.mysite.net/12345)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mysite\.net)$ [NC]
RewriteRule ^(.*)$ http://example.%1/$1 [R=301,L,NE]
I'm wanting to tweak this to work with Laravel, but it doesn't quite work the same considering Laravel is hosted out of the following path mysite.net/laravel/public rather than mysite.net/example.
How would I edit the above .htaccess to redirect all requests to mysite.net/laravel/public to laravel.mysite.net? I.e mysite.net/laravel/public/12345 would redirect to laravel.mysite.net/12345
Edit
Here is the Virtual Host I have added through Apache
<VirtualHost *:80>
ServerName laravel.mysite.net
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public>
Options -Indexes
</Directory>
</VirtualHost>
Place this rule as your very first rule inside /var/www/laravel/public/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?mysite\.net$ [NC]
RewriteCond %{THE_REQUEST} /laravel/public/(\S*)\s [NC]
RewriteRule ^ http://laravel.mysite.net/%1 [L,R=302]
You can use redirect rule of htaccess to redirect any directory to a different domain. Use the below code:
Redirect 302 /laravel/public/ http://laravel.mysite.net/
Please let me know if it helps you out.

htaccess redirection to one file for one domain and another file for another alias domain

I have 2 domains going to a vhost
example.com thisismyexample.com << this is an alias of the first one. It has got his own vhosts which is a symbolic link of the first one.
I need different behavior for each one so I'd like to redirect as:
if request comes for example.com -> example.com/index.php
if request comes for thisismyexample.com -> thisismyexample.com/admin.php
Both files index.php and admin.php are on the same directory which is my document root. At the moment there's a REQUEST_FILENAME rule and everything goes to index.php
I would try with redirectmatch but would like some opinions first
RedirectMatch ^example.com/$ http://example.com/index.php
RedirectMatch ^thisismyexample.com/$ http://thisismyexample.com/admin.php
Is this possible using htacces? If so, how would it be?
Use a RewriteRule with a RewriteCond that checks the host-header:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ http://%{HTTP_HOST}/index.php [R,L]
RewriteCond %{HTTP_HOST} ^thisismyexample.com$
RewriteRule ^ http://%{HTTP_HOST}/admin.php [R,L]

apache htaccess rewrite with alias

We are changing our domain name and this is meant to work for stand alone applications. In Apache virtual host file the DocumentRoot is /var/www/website/html, not /var/www/example/html as in this block:
Alias /apps/dept /var/www/example/html/apps/dept
<Directory "/var/www/example/html/apps/dept/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I put an .htaccess file in /var/www/example/html/apps/dept directory as follows:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example.orgname.state.tx.us/apps/dept [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
This seems to follow what is recommended here, http://httpd.apache.org/docs/current/mod/mod_rewrite.html and Apache : How to Use Rewrite Engine Inside Alias. I see no results. The new domain has a virutal host config in the VH file, also. This same basic rewrite works for our Drupal website which does not use an alias. What changes might be necessary to have the domain name rewritten with an appended application pathname? Is the RewriteBase incorrect?
Thx.
So you only want to redirect /apps/dept/, correct? This should work. Place it as an .htaccess or in the Apache config for example.orgname.state.tx.us and all should work as expected.
RewriteEngine on
RewriteRule ^/apps/dept/(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
So now, any requests going to this URL
http://example.orgname.state.tx.us/apps/dept/
Will now go to this URL:
http://example.orgname.texas.gov/apps/dept/
And any request parameters to the right of the URL will be passed along as well.
EDIT Just reread what you wrote here:
I put an .htaccess file in /var/www/example/html/apps/dept directory
as follows.
The .htaccess I described above should be placed in /var/www/example/html/ and not in the /apps/dept subdirectory.
But if you want the same behavior from an .htaccess placed in /apps/dept then use this:
RewriteEngine on
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
This way any request made from /apps/dept will to to example.orgname.texas.gov/apps/dept/ including subdirectories of /apps/dept such as /apps/dept/test_app1, /apps/dept/test_app2 or /apps/dept/test_app3.
Or perhaps try this:
RewriteEngine on
RewriteRule (.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
Note I removed the ^ which would force the RewriteRule to match the beginning of the URL.
You cannot match Request URI in %{HTTP_HOST} variable, it matches only domain name. Chang your rule to:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example\.orgname\.state\.tx\.us$ [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]

Using .htaccess file to redirect to a subdomain

In my root folder, I have a home directory. I'd like all requests for mydomain.com or www.mydomain.com to be forwarded to home.mydomain.com. In my cPanel, I have already set up the sub-domain home.mydomain.com to point to mydomain.com/home.
Here's what I currently have in my .htaccess file:
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteRule ^(.*)$ \/home\/$1 [L]
This successfully forwards mydomain.com and www.mydomain.com to mydomain.com/home or www.mydomain.com/home, respectively. But home.mydomain.com/filename gives me an internal server error, instead of serving the file at mydomain.com/home/filename.
I'm not sure I understand the exact requirements, but it seems you want to rewrite all (www.)mydomain.com requests to the /home directory while your subdomain home.mydomain.com already points to that directory and thus should be exempt from that rewrite directive. If for some reason (www.)mydomain.com can't be set up to point to /home as well, you'd need a Rewrite Condition, something like
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /home/$1 [L]
I also removed the backslashes which should not be needed. You can use the htaccess tester to check rewrite rules easily online.