hide subdomain in address bar - wordpress

My main website is located on www.example.com. On the subdomain sub.example.com I'm building another website. Both websites are build with WordPress. Both domains are registred at the same registrar
I registered a domainname (lets say: www.example2.com) wich I would like to point to sub.example.com. When someone visites www.example2.com he/she should see www.example2.com in the address bar and not sub.example.com.
I was trying to get this to work with a .htaccess file and it kind of worked, but not in the way I was hoping:
when I enter example2.com the browser opens sub.example.com and in the address bar it states example2.com (so far, so good)
when I navigate to another page on the website the address bar shows sub.example.com/another-page instead of example2.com/another-page
when I enter www.example2.com the browser opens www.example.com
This is how my .htaccess file looks like at the moment:
RewriteEngine On
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteCond %{HTTP_HOST} www.example2.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com [R=301,L]
Can someone tell me how I can get this to work?

Try this and see if it works for you. Also I would make sure the www dns record for example2.com is pointing to the same location as example2.com.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [L]
Edit:
Really for what you are trying to accomplish, I would suggest NameBased virtual Hosts if possible. That's what it was designed for. So that you can use the same IP for multiple sites. This change is done in the apache config file. See this simple example.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /www/example.com/html
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
ServerAlias example2.com
DocumentRoot /www/example.com/html/sub
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/name-based.html
That tells the server what document root to use for each website and therefore eliminates the need for these mod redirects and CNAMEs to sub domains.

Related

Redirect Server Aliases to Server Name Over HTTPS on Apache

I have been looking around for an answer to my problem for a while and have read various other answers without coming across one that works for me.
The problem I am trying to solve involves redirecting access to a WordPress site installed on an Apache server. The WordPress site is one of many installed as part of a WordPress Network installation and the site is setup on the domain https://www.mysite1.co.uk.
It is also necessary to access the site using a number of server aliases, which are to be redirected to the root domain. I have created the additional domains as server aliases in the Apache configuration, and have set redirects to ensure that if one of the aliases is entered then it should be redirected to the root domain.
<VirtualHost *:80>
ServerName www.mysite1.co.uk
ServerAlias mysite1.co.uk www.mysite2.co.uk mysite2.co.uk
...
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.mysite1.co.uk%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName www.mysite1.co.uk
ServerAlias mysite1.co.uk www.mysite2.co.uk mysite2.co.uk
...
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.mysite1.co.uk%{REQUEST_URI} [R=301,L]
SSLEngine On
SSLCertificateFile "certs/mysite1.co.uk.crt"
SSLCertificateKeyFile "certs/mysite1.co.uk.key"
</VirtualHost>
This configuration works whenever the user enters one of the aliases using http, they are redirected to https://www.mysite1.co.uk/. However, if they enter the aliases using https, instead of being redirected to https://www.mysite1.co.uk/, they are not being redirected at all.
Have I missed something in the configuration, or do I need to separate the https configuration and add the aliases into their own virtual host to do the redirect.
The sites are hosted on CentOS 7.9.2009 and running Apache 2.4.6.
If I got it correctly, you're trying to redirect every known domain reaching your apache to just one domain and when the connection comes as HTTP you want to redirect it to HTTPS.
To keep the configuration simple, I would create 3 different virtual hosts:
The first one will listen on http and include every domain you want to redirect (including the main one becuase it's listening on http). This virtualhost will redirect everything to the main domain over https:
<VirtualHost *:80>
ServerName main.site.com
ServerAlias mysite.example.com
ServerAlias another.site.com
Redirect permanent / https://main.site.com/
</VirtualHost>
The second one will listen on https port and includes all the domains but the main one and redirect it to your primary domain (it's like the previous one except it doesn't include the main domain)
<VirtualHost *:443>
ServerName mysite.example.com
ServerAlias another.site.com
Redirect permanent / https://main.site.com/
...
SSLEngine On
...
</VirtualHost>
In the end, the main one will just listen on https and will include only the main domain:
<VirtualHost *:443>
ServerName main.site.com
...
SSLEngine On
...
</VirtualHost>
This way, the only virtualhost requiring configuretiona (document root, locations, etc.) is the last one.

IP canonicalization - When IP adress is typed I want it to direct to the domain name

I have Ubuntu 20.04.1 Apache 2.4.41 webserver(digitalOcean) with SSH root access and domain name form GoDaddy.
Running Wordpress version 5.4.2, everything is working fine but I can't figure out how to do IP canonicalization. I have tried several methods with .htaccess but nothing works for me. When IP address is typed I want it to direct to the domain name.
You can use below rewrite rule in httd.conf file.
<VirtualHost XX.XX.XX.XX>
DocumentRoot "/var/www/html"
ServerName test.com
# Other directives here
RewriteEngine On
RewriteCond %{HTTP_HOST} !^test.com$
RewriteRule ^(.*)$ http://test.com$1 [R=301,L]
</VirtualHost>

Redirect HTTP domain to HTTPS domain via htaccess file code

Please let me know how I can redirect http domain to https domain hassle free via htaccess file. I want to redirect this domain
http://example.in to https://example.com
domain.
Please help me out.
Thanks in advance!!!
Apache doesn’t recommend to use this in the .htaccess, you can read about it here.
You should use this instead:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
In case that you want to use .htaccess anyway you need to use the next rewrite:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Subdomain always redirect to WWW (home page)

I want, when open subdomain to return existing Wordpress page ex. www.example.com/custom-wordpress-page.
Server OS is CentOS 6.6, while Web Server (httpd) is Apache 2.2.15.
It have installed and configured Wordpress with .htaccess files configured for it's mod_rewrite.
Root folder is /var/www/html.
.htaccess file is in /var/www/html/.htaccess :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This configuration is working very well with one domain (ex. example.com). Today I wanted to add subdomain (ex. subdomain.example.com).
I tried multiple solutions including VirtualHosts with/without NameVirtualHost in httpd.conf file, also adding RewrriteCond&RewriteRule
under mod_rewrite in .htaccess file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName subdomain.example.com
ServerAlias subdomain.example.com
Redirect permanent / http://example.com/custom-wordpress-page
</VirtualHost>
Also in first VirtualHost, I tried *.example.com instead off www.example.com.
In second one, I tried to make Reverse Proxy using ProxyPass and ProxyPassReverse.
Instead of using :*80 I tried using IP address of server and domain name.
Using .htaccess Rewrite rule with/without VirtualHosts gave no results.
subdomain.example.com always opens www.example.com instead of www.example.com/custom-wordpress-page.
Keep in mind - there is a lot of threads like this, but I tried many of them, including many tutorials but they aren't working alongside Wordpress.
Edit : Want to add that there are no wildcards in DNS records. There are NS records, one A record for example.com, one A record for subdomain.example.com and CNAME record for www that is poiting to example.com. There are NO *.
There was problem with hosting provider DNS records, configuration was correct, but I had to call provider to check records in their system and fix what is broken. After they done their job, it works correctly.

Bitnami Wordpress httpd-vhosts.conf url rewrite issue

I am having some issues with URL rewrite from sub domain to sub directory of a wordpress site. I didnt set the WP stuff up just inherited the problem. The box is a Bitnami WordPress.
I am attempting to do some redirects with url masking in the vhosts config. The redirects are fine just not getting the URL to mask and ending up with ugly sub dir in the URL.
This is what I have so far in /opt/bitnami/apps/wordpress/conf/httpd-vhosts.conf
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/Ugly_sub_dir [P]
RewriteCond %{HTTP_HOST} ^app\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/Another_ugly_sub_dir [P]
And in both cases I am redirecting fine but ending up with URL as:
domain.com/Ugly_sub_dir
domain.com/Another_ugly_sub_dir
But would like to see the plain old..
www.domain.com
app.domain.com
Any help much appreciated
OK I was approaching this in entirely the wrong way. Solved by setting up a proper vhost (not sure why I didnt do this in the first place - wasnt thinking, its been a while) ..
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs/Ugly_sub_dir"
Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

Resources