Rewriting specific host to directories on localhost - apache

I'm currently trying to redirect my local configured domains to its directories.. What is the best way to achieve this using htaccess?
Currently 'localhost' redirects to /Library/WebServer/Documents/ (mac OSX), i want to create an htaccess file in this directory that will lead all domains to its directory.
Example htaccess code of what I'm looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com/$
RewriteRule ^(.*)$ /Library/WebServer/Documents/example.com/$1 [L]
I hope someone can help me out, think there's an easy fix for this!
Kind regards,

This is not possible with .htaccess alone. You need access to the main server configuration file or at least modify the appropriate virtual host section.
If you can modify the main config file, you can add any number of virtual hosts.
If you're allowed to adjust your virtual host only, you must add as many ServerAlias entries to your virtual host as you have domains.
Then you can add the rewrite rules for the various domains
RewriteEngine On
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/example.com/$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/domain.com/$0 [L]

Try this
# get the server name from the Host: header
UseCanonicalName Off
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /var/www/vhosts/%0/httpdocs
<Location /var/www/vhosts>
AllowOverride All
Options +FollowSymLinks
</Location>
Then, you could try these examples:
example.com would show /var/www/vhosts/example.com/httpdocs
example2.com would show /var/www/vhosts/example2.com/httpdocs
Remember to add the domains to your hosts file.
Source

Related

Stop virtualhost rewrite rules executing if a rule was matched in the global config

I have the following setup.
Apache running with a separate VirtualHost file for each site.
Each of these has their own set of rewrite rules, for http to https for example which is all running fine.
What we would like to happen is this, from the global config we need to be able to check if a request is for a particular subdirectory. If it is then we should allow this request to process as it should but at that point we do not want the individual virtual host file rewrite rules to kick in. Therefore allowing this directory to be served on non https connections and not be redirected to https.
I have set up the rewrite rules and can match on the directories and redirect to an external url if it matches from the global which shows its inheriting but if I try to just allow it through the virtual hosts rewrites kick in and it redirects.
I have tried using L and END but this did not work either.
Is there any way of achieving this without editing the virtual host files that are already configured?
Main httpd config entry
<Directory "/www">
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride All
allow from all
Order allow,deny
Require all granted
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/sub_directory/$ [NC]
RewriteRule ^(.*) $1 [L,END]
#RewriteRule ^(.*) - [L,END]
#RewriteRule ^(.*) http://www.google.com [L,END] # This does get triggered
</Directory>
sample virtual host file.
<VirtualHost *:80>
ServerName urlone.com
ServerAlias urltwo.com
DocumentRoot /www/
RewriteEngine On
# redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://urlone.com$1 [R=301,L]
</VirtualHost>
so if I visit urlone.com it should redirect to https://urlone.com but if I visit urlone.com/sub_directory it needs to not allow the redirect to https.
I hope this makes sense to someone and thanks in advance for any help.
In global httpd.conf:
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/sub_directory$ [NC]
RewriteRule ^ - [E=PATH_MATCHED:true]
(if needed, you can add additional rules, or additional flags to the above rule)
In virtual_host.conf
RewriteCond %{ENV:PATH_MATCHED} !=true
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://urlone.com%{REQUEST_URI} [R=301,L]

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.

How to rewirte url to visit file under another folder

How to rewrite if file after stylesheets then read file under specific folder
e.g
if user visit
domain1.com/stylesheets/index.css actually read > domain1.com/app/assets/stylesheets/index.css
or
if visit
domain1.com/stylesheets/bundle/index.css read > domain1.com/app/assets/bundle/stylesheets/index.css
I tried below code but not work...
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com\.localhost\$ [NC]
RewriteRule ^/stylesheets/(.*)\.css$ app/assets/stylesheets/$1
#or this rule not work too
RewriteRule ^/stylesheets/$ app/assets/stylesheets/$1
<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/domain1.com"
ServerName domain1.com
</VirtualHost>
First you need to check the HTTP_HOST against the host name (without any further stuff, that is):
RewriteCond %{HTTP_HOST} ^domain1\.com\$ [NC]
Second, in order to be able to process the query further, you will need to capture parts of it (in parentheses, that is).
You can use this rule in vhost config or in site root .htaccess:
RewriteEngine On
RewriteRule ^/?(stylesheets/.+?\.css)$ /app/assets/$1 [L,NC]
If using vhost config remember to restart Apache after making this change. This is assuming app/assets/ path exists as full filesystem path of /Users/username/Sites/domain1.com/app/assets/

Pointing subdomains to a "directory"

I have two questions regarding pointing subdomains to a directory:
Currently I run local, but I can run my site on a fake domain i have set up (with hosts file), its called mysite.com. How can i (by server settings?) do so All subdomains will point to / ? Example anders.mysite.com should show mysite.com and asdassdd.mysite.com also.
Maybe 1. is not necessary, but how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.
Why i thought of 1. is because I do not want to specify anywhere in the htaccess or any apache/domain setting, what the subdomain are, since this will be dynamic (created by logged in users in my webapplication)
Currently the users can use mysite.com/anders/ which is a URI they have created, and not a real directory. In the Kohana bootstrap I am then grabbing the URI and showing the relevant user page.
I am using Kohana MVC framework and have this in my htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Any help appreciated!
For 1, in your vhost just add a ServerAlias:
ServerAlias *.mysite.com
Then any subdomain (www included) will get pointed to the same document root.
If you want to put subdomains in their own directory (and since you have access to server config), you can use mod_vhost_alias and the VirtualDocumentRoot directive:
VirtualDocumentRoot /path/to/your/htdocs/%1
So if you request blah.mysite.com, you'll end up in /path/to/your/htdocs/blah as the doc root.
If it's a matter of you needing directory names of non-existent directories in the URI so that Kohana can route them, you'll need to make sure you have mod_proxy loaded:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,P]
To proxy the request back to Kohana with the right URI.
how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.
You can use this rule:
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
RewriteRule ^ /%1%{REQUEST_URI} [L]

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.