.htaccess redirect dynamic domain to folder - .htaccess

For local development I've setup my router to redirect all requests to *.lan to my local xampp server. Now I have to setup virtual hosts for every domain I want to use. This works, but I don't want to setup a new virtual host (pointing to the corresponding folder) for every project and restart apache.
Is it possible to create a .htaccess file to "catch" a domain, and rewrite it to the folder, so I can use the domain as the folder name? For example:
example1.lan loads the website located in /example1/
example2.lan loads the website located in /example2/
This tread ( Apache - Domain for localhost to access folders as http://folder.local ) does almost the same thing, but with a redirect. I don't want a redirect, I want rewrite.
Thanks in advance!

Try adding these rules to the htaccess file in your document root (the directory where the example1 and example2 directories are):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.lan$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L]

Related

Symfony .htaccess

I configured a test folder for websites in hostgator, so far I have no problems in some flatform like wordpress,magento,joomla, I did not even change any seetings in .htaccess to make it run. Recently, I am working on a symfony2 project and upload my test application in the test folder I created. This is how I create a test folder
mywebsite.com/tests/wordpress_site(no problem)
mywebsite.com/tests/joomla_site(no problem)
mywebsite.com/tests/symfony2_site(403 forbidden error)
In my WAMP, I can accessed my symfony project
localhost/symfony_site/web/
I did not touch the .htaccess since this is just a test site in local machine.
How would you configure a symfony2 project in this setup?
mywebsite.com/tests/symfony_site
It is almost impossible to create clean Urls when installing symfony in subfolder...
With Apache, the better solution is to use a VirtualHost which have tests/symfony_site/web as DocumentRoot, and to use the built-in .htaccess file (which uses url rewriting module, or if it not available, uses a 302 redirection to app.php/)
But If you can't add a virtualHost to your server, it is hard...
An other way is to add a .htaccess at tests/symfony_site and put DirectoryIndex: web/app.php
(https://httpd.apache.org/docs/current/mod/mod_dir.html)
So you can have clean URLs... but be careful with this solution: the urls parsed by the router are relative to your site root (and not to symfony root...) so the router might be lost... (e.g if you have a root named /test you should rename it /tests/symfony_site/test... And it is the same for assets, you have to add the site directory, otherwise they will return a 404 error...
In my case, I just use existing .htaccess in web folder (or I create a new one with a DirectoryIndex Instead), so my app work with URLs like mywebsite.com/tests/symfony_site/web/
I hope this helped,
Vincent
You can try typing the following code into the .htaccess file to the symfony root folder
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(img|files|images|favicon.ico|css|js|robots\.txt)
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{HTTP_HOST} host.com
RewriteCond %{REQUEST_URI} !web/
RewriteRule ^(.*)$ web/$1 [L]

Using wildcard subdomain on specific directory

I am setting up an existing wordpress install to use multisite. I have successfully set up the network and since it was an existing install it uses subdomains (which I wanted anyways).
I have created a new site/subdomain on my network so it would look something like this example.domain.com
The issue I am running into is getting wildcard subdomains setup on my hosting provider BlueHost.
I am hosting multiple domains so when I setup a wildcard it always points to the root or original ip address.
I need it to point to a specific directory of the root so it uses the correct wordpress install. root/specific-directory
Is there a way to achieve this? I am not too familiar with a and cname issues. If there is a way to achieve it by writing in the htaccess that would be okay too.
I am hosting multiple domains so when I setup a wildcard it always points to the root
I'm assuming you meant that you get the same content for example.domain.com as if you were visiting www.domain.com. This can surely be handled through htaccess rewrite rules.
If a sub-domain named subsite was to be served out of ${DOCUMENT_ROOT}/subsite directory, you would add the following rules to your .htaccess placed at your web root / directory:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subsite\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subsite [NC]
RewriteRule ^(.*)$ /subsite/$1 [L]
If there are several mutisite subdomains that need redirection, use the following dynamic rules instead:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*?::\1
RewriteRule ^(.*)$ /%1/$1 [L]
You would also need to add ServerAlias *.domain.com to your <VirtualHost> configuration in Apache's httpd.conf file.
It seems BlueHost doesn't take up any requests to modify httpd.conf directly for you. But, they do provide a way to setup a wildcard subdomain from their Control Panel.
Take a look at How to Set Up a WordPress MultiSite in Bluehost. The last section outlines the process with screen shots. You also need to enable theme per site for your subdomains.
1- You can add your subdomain as an addon domain and it can have a separate subdirectory as you want.
2- Another way to achieve this is using another host for subdomians. On the second host you set up your subdomains and point your subdomains from your main host to subdomains on the secondary host.

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

How to setup .htaccess to rewrite to a different folder

I'm moving my site to a new host but I need to have my current server continue to handle requests (not all files can be moved to the new server).
So I added a parked domain to my old server (old.mydomain.com) and I want all requests to it to be written to the files from the old site.
My old site (mydomain.com) was hosted internally in a folder (/public_html/mydomain/) and I want all requests to old.mydomain.com to be rewritten to the same folder.
So if mydomain.com/blog was internally at /public_html/mydomain/blog, I now want old.mydomain.com/blog also to reach /public_html/mydomain/blog.
Here is the .htaccess that I'm trying to use:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com/*
RewriteRule ^(.*)$ mydomain/$1 [NC,L]
But for some reason as soon as I add the $1 in the rewrite rule I get an internal error.
Any ideas?
Configure this as a separate vhost called old.mydomain.com and ensure it comes before *.mydomain.com in your vhost definitions (i.e. higher in vhosts.conf). Give old.mydomain.com the same DocumentRoot as your previous domain had.
.htaccess is the most processor intensive way to serve a webpage, and should only be used there are no other options available.
Please try to fix your .htaccess config as follows:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com$
RewriteRule ^(.*)$ /public_html/mydomain/$1 [NC,L]

Creating SubDomains to Absolute Paths with .htaccess

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Resources