Drupal 7 setting different Hosts for different request headers - .htaccess

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.

Related

.htaccess rewrite subdomain keeps original url

I am writing a .htaccess rule that sends a subdomain request to a specific login page file. My current rule is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.name.com$
RewriteRule ^ https://name.com/app/login [R=302,L,NE]
This works as I would expect. I now want to build on this so as to keep the original url displayed. The RewriteRule would continue to redirect to https://name.com/app/login but still display https://subdomain.name.com as the URL within the web browser.
Is this possible within .htacess file? I cannot find a solution.
You could use:
RewriteCond %{HTTP_HOST} ^subdomain.name.com$
RewriteRule ^ https://name.com/app/login [P]
For this to work, you need to
1) enable apache proxy module
2) enable SSLProxyEngine in the VirtualHost definition:
<VirtualHost subdomain.name.com:80>
...
SSLProxyEngine on
</VirtualHost>
<VirtualHost subdomain.name.com:443>
...
SSLProxyEngine on
</VirtualHost>
3) If the certificate is not "trusted", e.g. is self signed, you could disable the proxy `s verification by adding:
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
to the virtualHost definitions above.
References
Redirect website but keep the original url in the address bar
Disable proxy ssl verification

Incomplete htaccess for subdomain

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

godaddy subfolder domain htaccess redirect without changing browser url

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

.htaccess subdomain redirect problem

I have a website let's say www.example.com and I have a subdomain something.example.com
Both main domain and sub domain are pointing to directory "public_html"
I have .htaccess on root which redirects any URL without www to www.
For e.g. if user enters example.com then he will be redirected to www.example.com
If user enters example.com/mypage.html then he will be redirected to www.example.com/mypage.html
Now the problem is this is also affecting my subdomain. Because if someone enters something.example.com/visit.html then he is redirect to www.example.com/visit.html
I don't want this! If user enters subdomain then I don't want to redirected to www domain. This is what I have in my .htacces file
RewriteCond %{HTTP_HOST} !^www.stackoverflow.com$ [NC]
RewriteRule ^(.*)$ http://www.stackoverflow.com/$1 [R=301,L]
Can you please tell me what should I do to solve above problem?
Thanks.
Do you have access to your webserver configuration? This is better done by configuring the webserver.
On apache you would configure one virtual domain like this:
<VirtualHost *:80>
ServerName somedomainiwanttoredirect.com
ServerAlias maybe.somemoredomainstoredirect.com
ServerAlias orsubdomains.toredirect.com
RewriteEngine On
RewriteRule ^(.*)$ http://www.target.com/$1 [R=301,L]
</VirtualHost>
and on your real configuration, the www.target.com you add your subdomains that you do not want to be redirected:
ServerAlias subdomain.target.com

301 redirect is working for www.example.com but not for

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.

Resources