.htaccess subdomain redirect problem - .htaccess

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

Related

Domain name not redirecting

My website can be accessed via https://example.com but can not be found using either https://www.example.com or www.example.com, if I use either of these it just throws up a "site cannot be reached" page.
Is this something I can change in the .htaccess file or is it the DNS of the domain?
I have tried adding the following code to the .htaccess file, but it makes no difference
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
...or is it the dns of the domain?
Yes, this is "most probably" a DNS issue. The request is not even reaching your server by the sounds of it. .htaccess is consequently never processed.
You'll require a CNAME record that points the www subdomain to the domain apex (example.com) - so that both www.example.com and example.com point to the same place.
However, you also need to make sure that your server is accepting requests to both www.example.com and example.com - this doesn't happen by default. Although if you have configured your account via some hosting control panel then this has probably already been done for you. Otherwise, you need a ServerAlias record defined in the relevant <VirtualHost> container. For example:
ServerName example.com
ServerAlias www.example.com

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

Drupal 7 setting different Hosts for different request headers

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.

htaccess rewrite secondary domain to subdomain

I have a main domain "www.main.com" with subdomain "subdomain.main.com"
the subdomain is located in the folder /htdocs/subdomain/ and the main domain is a symbolic link to the /htdocs/www/ (which also serves www.main.com)
Now I have registered a new domain name, "newname.com" which I have connected to my hosting account. Now I am trying to determine the contents of /htdocs/.htaccess for rewriting incoming requests to subdomain.main.com.
To be clear, I do not want to redirect. I want www.newname.com/anything to be rewritten to subdomain.main.com/anything, while the user keeps on navigating newname.com.
I fear there is a catch with circular rewrites?
What should my .htaccess look like?
My document root is /htdocs and the subdomain and the new domain are on the same ip address.
First of all, make sure your DNS is properly setup i.e. ping www.newname.com goes to the same server (= same IP) than ping subdomain.main.com.
Then your vhost should look like:
<VirtualHost *>
ServerAdmin webmaster#papdevis.fr
DocumentRoot "/home/subdomain"
ServerName subdomain.main.com
ServerAlias www.newname.com
</VirtualHost>
Then everything should work transparently. It's all about vhosts. I don't know with htaccess, sorry.
It was in fact easy, i created a symbolic link to my new domain (primary.com/secondary) which links to the folder which also serves secondary.primary.com and I rewrite all queries to www.secondary.com to that symbolic link. Totally untransparent, just how I wanted it. It was just a bit fighting with drupal before it let me take control of a tiny part of my main domain :)
The main .htaccess is
RewriteCond %{HTTP_HOST} ^www.secondary.com [NC]
RewriteCond $1 !^secondary/
RewriteRule ^(.*)$ /secondary/$1
and i redirect all secondary.com to www.secondary.com in my rewrite base with this .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} secondary.com$ [NC]
RewriteRule ^(.*)$ http://www.secondary.com/$1 [R=301,L]

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