mod_rewrite http_host - wordpress

How can I prevent access to a subdirectory based on HTTP_HOST.
I have 2 parked domains pointing to one directory. They both serve up the same website.
However I wish each to have a seperate wordpress blog. To do this I'm installing them in different directories.
domain1.com/domain1-blog/
domain2.com/domain2-blog/
The trouble is the directories are accessible across domains. For instance
domain1.com/domain2-blog/
domain2.com/domain1-blog/
I REALLY don't want people to be able to access across domains. Wordpress will break and I will get duplicate content issues in Google.
What is the EXACT code I need to put into my htaccess file to serve up a 404 error should anyone try to access
domain1.com/domain2-blog/
OR
domain2.com/domain1-blog/
I thought maybe using HTTP_HOST combined with mod_rewrite somehow?
I'm a big newb when it comes to htaccess, so the more explicit you can be the better.
Thanks

Try adding the following to your htaccess file in the root directory of your domain
RewriteEngine on
RewriteBase /
#if its domain1
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
#and the request is for domain2 blog
RewriteCond %{REQUEST_URI} ^/domain2-blog/ [NC]
#send a 403 forbidden
RewriteRule . - [F,L]
#if its domain2
RewriteCond %{HTTP_HOST} domain2\.com$ [NC]
#and the request is for domain1 blog
RewriteCond %{REQUEST_URI} ^/domain1-blog/ [NC]
#send a 403 forbidden
RewriteRule . - [F,L]

Related

https on subdomain redirects to main domain; subdomain works without https

I have a website, https://www.example.com and subdomain, https://sub.example.com.
My problem is that whenever I try to visit https://sub.example.com I am redirected to https://www.example.com. But, when I visit http://sub.example.com, I am not redirected.
I have a wildcard SSL cert which should allow me to visit my subdomain with https://. Both sites run WordPress and are hosted with Bluehost. I have tried many solutions as well as contacting Bluehost customer support twice to no avail. I would like to be able to visit both my main domain and subdomain with SSL active.
I have edited the .htaccess file according to the following solutions found here: htaccess redirect domain to https, subdomain to http and www to non-www
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.example\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://sub.example.com%{REQUEST_URI} [R=301,L,NE]
https://my.bluehost.com/hosting/help/766
# Custom subdomain .htaccess SSL + WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
# End custom subdomain .htaccess
# Custom maindomain .htaccess WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# End custom maindomain .htaccess
http://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/
I have also tried fresh WordPress installs of both the main domain and subdomain with no luck. I'm not sure what else to try. Any help and suggestions would be much appreciated.
Several things to try:
First, when you're making changes, use a tool like Webconfs HTTP Header Checker. This way, if your web browser has redirects cached and you make a change that fixes the redirects, the Header Checker won't be fooled. Your web browser, on the other hand, will keep redirecting even after things are fixed, until you clear the cache. Alternatively, you can try a Chrome Incognito window.
Instead of using just the one .htaccess file, use one for each site. Assuming example.com is your primary domain, you can set rules for the "root" WP site in the root folder, and you'll set rules for the "subdomain" WP site in the folder where that separate WP install sits - /subdomain.com/.htaccess.
Make sure your "root" WP site URL and home URL are both set to the https version. You can do this in a couple of places: in wp-admin of the "root" WP site, go to Settings > General. The "WordPress Address (URL)" and "Site Address (URL)" both need the https. If they are set to http, it will log you out when you click save. Alternatively, you can do this in phpMyAdmin - in the wp_options table, look for the 'siteurl' and 'home' options and make sure they're https rather than http.
It sounds like your "root" WP was installed when the site was insecure, running on http. Sometimes it takes more than just updating the site URL and home URL. Try using WP Migrate DB which will do a search-and-replace to convert all instances of your http URLs to https URLs.
Once all that is done, if the "root" site still redirects to the non-secure version, you can install a plugin such as WordPress Force HTTPS.

Redirecting url to access subdirectory

I have a wordpress subdirectory install in a directory called 'wordpress'. So currently I am able to access the live site as follows: www.domain.com/wordpress.
My objective is for users to simply navigate to: www.domain.com, omitting the wordpress directory from the url.
I was wondering if someone can help me configure my .htaccess so every time a user navigates to anything with domain.com/wordpress it'll redirect to domain.com.
So for example: domain.com/wordpress/admin will become domain.com/admin
I created a new .htaccess file and placed in the root with the following contents, which did not work :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ wordpress [L]
To rewrite / to /wordpress you can use this rule :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteRule ^(.*)$ /wordpress/$1 [L]
This will internally redirect all requests from root to the subfolder.
Do not remove the RewriteCond directive otherwise the the rewrite destination will rewrite back to itself causing an infinite loop error.

.htaccess too many redirects. Redirect all users except ip to different part of website

I'm trying to make a website where all users are redirected to a specific part in the domain so that they can see a "comming soon" page. While some ip addresses can still access the normal website so that they can see how it is going to look.
What I'm trying to do: create a wordpress website in the root of public_html, create a soon directory in public_html and redirect all users except a few ip's to http://www.domain.com/soon/index.html
My .htaccess looks as followed:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.2.3.4
RewriteRule (.*) http://www.domain.com/soon/index.html [R=302,L]
However whenever I go to the website to test it out it tells me:
ERR_TOO_MANY_REDIRECTS
Is there something I'm doing wrong in my .htaccess?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
RewriteRule .? /soon/index.html [L]
Use this rule as your very first rule in your .htaccess:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(1\.2\.3\.4|11\.22\.33\.44)$
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$
RewriteRule !^soon/index\.html$ http://www.domain.com/soon/index.html [R=302,L,NC]
Variable REMOTE_ADDR matches client's IP in a web request.
RewriteCond %{REQUEST_FILENAME} !-f is needed to avoid redirecting for css/js/image files and /soon/index.html file.

htaccess redirect not redirecting index.html page

I have a client project that requires a specific domain name to be redirected to another directory, while the rest of the site is a standard WordPress installation.
My redirect appears to be written properly, and is placed at the very top of the htaccess file:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old_domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old_domain\.com$ [OR]
RewriteRule ^/?$ "http\:\/\/www\.new_domain\.com\/directory\/" [R=301,L]
The problem is that it won't redirect http://old_domain.com/index.html
Instead, it sends the user to WP's 404 page.
What am I doing wrong?
UPDATE:
After receiving an otherwise helpful suggestion, I thought some additional context would be in order.
This is a real estate project. The main WP site (new_domain.com) is for condo sales, and they have a separate external site for condo rentals.
They have a third legacy domain (old_domain.com) that they want to redirect to a portal page (new_domain.com/directory), where the user can select buy or rent, and then proceed to the appropriate site.
So, we don't want all requests to be redirected, just any that use the legacy domain.
The following rule should work. It should redirect all requests from www.old_domain.com and http://old_domain.com to http://www.new_domain.com/directory
RewriteEngine On
RewriteRule ^(.*)$ http://www.new_domain.com/directory/$1 [R=302, L]
Change 302 to 301 when you are sure the redirect works
EDIT:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old_domian.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old_domain.com$
RewriteRule (.*)$ http://www.new_domain.com/directory/$1 [R=302,L]
You must have RewriteEngine On. You can consolidate the two www. and non-www. tests. Don't put an [OR] after the last RewriteCond.
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com$
Is new_domain.com an "add-on" to old_domain.com, so that both pass through the same .htaccess file, but with different domain names? Or are these physically separate systems, with their own .htaccess files? That is, what URLs are going to pass through this .htaccess file?
Your current code will redirect anything coming in on old_domain.com to /directory/<original URI+Query String> on new_domain.com, and tell search engines and users to update their entries and bookmarks. Is that what you want?

Advanced HTACCESS Subdomain Redirection (foo.dom2.com to foo.dom1.com)

I have domain1.tld and domain2.tld. Domain1.tld is the primary domain name and domain2.tld is merely an alias. I have domain2.tld successfully redirected to domain1.tld, via HTACCESS, but I want all subdomains on domain2.tld to likewise redirect to their parallel subdomain on domain1.tld, dynamically.
For example: bla.domain2.tld should redirect to bla.domain1.tld, and foo.domain2.tld should redirect to foo.domain1.tld.
This solution needs to be dynamic, not hard-coded to specific subdomains.
Haha, I guess what I'm writing is a catch-all, one-to-one, subdomain-inclusive, alias domain redirection script in HTACCESS.
Information:
I am on Bluehost, without access to VHOSTS, HTTPD.CONF, etc.
I have a Wordpress subdomain network (multisite). This means my domain1.tld subdomains are virtual. Wordpress handles this, so as long I can get foo.domain2.tld fully redirected to foo.domain1.tld, Wordpress should be none-the-wiser and work properly. Right?
Wordpress has the "Professional Domain Mapping" plugin. This allows the sites on my network to have their own domain names. But the issue at hand only concerns sites on my network that don't have their own domain names, and thus utilize subdomains on my primary domain (domain1.tld).
Thus, I have a wildcard subdomain set up for domain1.tld.
But I also have a wildcard subdomain set for domain2.tld, just to make sure that all subdomains on domain2.tld are going to the same IP address as domain1.tld: my thinking being that then these subdomains should be readable by the HTACCESS in my root folder.
This is the rule in my HTACCESS that I'm expecting to do this:
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+\.)?domain2\.com$ [NC]
RewriteRule ^(www\.)?([a-z0-9-]+\.)?(.*) http\:\/\/%2domain1\.com\/ [L]
But this isn't working. What am I missing?
I have also tried using an environmental variable, like this:
RewriteCond %{HTTP_HOST} ^(www\.)?[E=subdm:([a-z0-9-]+\.)]?domain2\.com [NC]
RewriteRule ^(.*) http\:\/\/%{ENV:subdm}domain1\.com\/ [L]
...but this just plains breaks both domains. :D
I'm relatively proficient with regular expressions, hence how comfortable I am working with HTACCESS, but I'm actually quite new to the world HTACCESS. I need another pair of eyes!
↓ UPDATE (2011-11-08 9:33am EST) ↓
I tried the first solution Jon Lin suggested, and had to edit it slightly. Originally, it wouldn't handle domain2.com without a subdomain until I put the period inside the second match parenthesis pair and followed the pair with a question mark.
As you can see below, I have gone ahead and provided the entire contents of my HTACCESS, in case you can tell that something is conflicting.
Currently redirecting properly with the suggested RewriteRule: www.domain2.com, domain2.com, and domain2.com/foo
Currently NOT redirecting with the suggested RewriteRule: foo.domain2.com
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+\.)?domain2\.com$ [NC]
RewriteRule ^(.*)$ http\:\/\/%2domain1\.com\/$1 [L,R]
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
# END WordPress
You're on the right track with %{HTTP_HOST} but you need to use backreferences (%1, %2, etc) to access the match against it. You want to do something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain2\.com$ [NC]
RewriteRule ^(.*)$ http://%2.domain1.com/$1 [L,R]
Note that if you try to access: http://www.sub.domain2.com/foo it will redirect you to http://sub.domain1.com/foo, The "www." of the original request is gone. If you want the www, change the rule to:
RewriteRule ^(.*)$ http://%1%2.domain1.com/$1 [L,R]

Resources