I've domain using wildcard dns but I want my main site example.com will grab content from folder /home/ by example.
If user type http://www.example.com/
or http://example.com/ site will read
file from subfolder /public_html/home/
If http://subdomain.example.com/ will reading content from /public_html/.
Let me know how to that in my .htaccess.
Try this rules:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule !^home/ home%{REQUEST_URI} [L]
you don't need htaccess or mod-rewrite for that. just config your site's document root dir to be /var/www/home/ or whatever you want it.
for example, in apache2 you can put this in apache-dir/sites-enabled/000-default/
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin mail#domain.com
ServerName domain.com
DocumentRoot /var/www/home/
<Location "/">
allow from all
</Location>
</VirtualHost>
Related
I'm not sure if it can be done and I've looked high and low. It's possible I'm just not constructing the search query correctly to yield the results I need.
I have 2 domains with differing content on a host (one added onto the other). When I added the second domain on, all 3 of these scenarios would resolve:
(1) domain2.domain1.com
(2) domain1.com/domain2/
(3) www.domain2.domain1.com
(4) www.domain1.com/domain2/
(5) domain2.com
I only want to show content for https://domain1.com or https://domain2.com -but-
https://domain1.com/domain2/ and https://www.domain1.com/domain2/ should resolve to https://domain2.com
The following changes to the .htaccess took care of scenario (1) & (3) but I cannot figure out how to handle scenarios (2) & (4):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2.domain1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain2.domain1.com [NC]
RewriteRule ^(.*)$ https://domain2.com/$1 [L,R=301,NC]
Any assistance would be greatly appreciated.
You need to create four virtual hosts, < VirtualHost >, for the same.
For domain1.com, you need to create a virtual host that redirect the http to https and another one that will take care of https connections.
Similarly for the domain2.com
ServerName and ServerAlias directives will help you in your problem (Name based virtual host).
More can be read for the same at link
Now your file will contain the below rules
<VirtualHost 192.168.23.34:80>
ServerName domain1.com
ServerAlias www.domain1.com
Redirect permanent / https://domain1.com/
</VirtualHost>
<VirtualHost 192.168.23.34:80>
ServerName domain2.com
ServerAlias www.domain2.com
Redirect permanent / https://domain2.com/
</VirtualHost>
<VirtualHost 192.168.23.34:443>
ServerName domain1.com
ServerAlias www.domain1.com
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/domain2
RewriteRule (.*) https://domain2.com/ [L,R=301,NC]
// ssl certificate setting for the same
</VirtualHost>
<VirtualHost 192.168.23.34:443>
ServerName domain2.com
ServerAlias www.domain2.com
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/domain1
RewriteRule (.*) https://domain1.com/ [L,R=301,NC]
// ssl certificate setting for the same
</VirtualHost>
Please make sure to restart the apache.
After this rule
www.domain1.com will redirect to https://domain1.com
domain1.com will redirect to https://domain1.com
www.domain2.com will redirect to https://domain2.com
domain2.com will redirect to https://domain2.com
https://domain1.com/domain2 will redirect to https://domain2.com
https://domain2.com/domain1 will redirect to https://domain1.com
https://www.domain1.com/domain2 will redirect to https://domain2.com
https://www.domain2.com/domain1 will redirect to https://domain1.com
I try to read many of the answer regarding htaccess but I could not find my way out.
Image this architecture in webhosting: root/public_html/subfolder
Now with the following code in htaccess file (placed in public_html) I can manage that when the domain is type in browser the visitor land in subfolder without digit example.com/subfolder, example.com is enough. It works.
What I would like now is that the visitor will be redirect not in subfolder but in an "extrafolder" placed inside the root (level root that is the same level where the folder public_html is placed but not inside public_html).
So how to modify this code:
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /subfolder/index.html [L]
I try changing the /subfolder/ with ../extrafolder/ (supposing the double dot will push up one level) but does not work.
Any idea?
Thank you
The current configuration you put is just working in case you put .htaccess inside the root folder of your website, which in this case is public_html, so no way to access another folder outside that root, except if you have directory alias.
In case you have access to change your apache configurations
You need to have the following steps to completely control the root of your website.
1- Yur apache virtual host configuration (this should be in /etc/apache2/sites-enabled/example.conf) should be something like the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test
<Directory /var/www/test/>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
3- You need to add this Alias /externalfolder/ "/var/www/test/externalfolder/" to the virtual host configuration above
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test/public_html
Alias /externalfolder/ "/var/www/test/externalfolder/"
<Directory /var/www/test/public_html>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now you can access that via http://example.com/externalfolder/file.php
Godaddy Deluxe Hosting with Linux
Main domain: www.domain1.com & 2 other hosted domains (www.domain2.com and www.domain3.com) are in subfolders
The contents of www.domain2.com .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule shop/tackle/(.*)/(.*)/ https://www.domain2.com/shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
This will direct www.domain2.com/shop/tackle/1/0/
TO
www.domain2.com/shop/products.php?subcat_id=1&subcat2_id=0
but changes the url. I do understand that is because I'm includeing the https://www.domain2.com in the rewrite. However I try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule shop/tackle/(.*)/(.*)/ shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
I get a 404 error. I does indicate that URL /domain2/shop/tackle/1/0/ can't be located.
Obviously the rewrite is including the domain2 folder name. I'm trying to keep the requested URL www.domain2.com/shop/tackle/1/0/ in the URL.
What I am missing?
If you have each domain pointing to correct DocumentRoot. as example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain1.com
DocumentRoot "/var/www"
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
DocumentRoot "/var/www/domain2"
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain3.com
DocumentRoot "/var/www/domain3"
</VirtualHost>
Then I don't think how the /domain2/ would be added onto relative request. But, if it is, then you should try RewriteBase directive to help with relative substitution is built correct. Like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule shop/tackle/(.*)/(.*)/ shop/products.php?subcat_id=$1&subcat2_id=$2 [NC]
This link has more information:
http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritebase
I have a Drupal 7 website, some resources are being requests with the Host set to have the www prefix while others are not.
https://www.example.com when request header is 301 Moved Permanently
https://example.com when request header is 304 Not Modified
Changing the base_url does not appear to modify the behaviour.
Is there anyway I can make drupal set the Host as https://example.com for all the request headers?
There's an Apache wiki page, which shows a solution to your requirement. The first solution uses a virtual host setup and the Redirect directive
# Redirect every request to example.com
<VirtualHost *:80>
ServerName www.example.net
ServerAlias www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
# Define virtual host for example.com
<VirtualHost *:80>
ServerName example.com
DocumentRoot /usr/local/apache/htdocs
</VirtualHost>
And the second setup uses a mod_rewrite redirect
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R]
You can choose whichever fits your environment best.
I am getting a strange problem with 301 redirect it the code is working for with www in the link and not working for with out www
RewriteBase /
RewriteRule ^api$ http://example.com/topic/api [R=301,L]
if i type http://www.example.com/api it works but if i do http://example.com/api it doesnt work
Adv thanks
prasanth
Check your Virtual Host configuration should be like below
<VirtualHost *:80>
...
ServerName www.example.com
ServerAlias example.com
...
</VirtualHost>
If the page is not found, it is possible that the example.com and www.example.com point to difference directory, otherwise, example.com may not linked to any place yet.