I have a Wordpress multisite with a primary domain that is used for admin site management and then hundreds of other sites that each have their own unique domain (NOT a subfolder or subdomain of the primary site).
I need to redirect each site to its corresponding landing page on our main site. I am programmatically generating these redirect rules since there are hundreds of redirects that need to be set up. I am then adding those redirect rules to one master .htaccess file at the document root.
Here is what I have so far:
RewriteCond %{HTTP_HOST} ^(.*)?firstsite.com [NC]
Redirect 301 / https://newsite.com/1234/
RewriteCond %{HTTP_HOST} ^(.*)?secondsite.com [NC]
Redirect 301 / https://newsite.com/5678/
What I'm seeing is that it is redirecting all traffic from any of the sites to the first "Redirect 301" address (https://newsite.com/1234/). Presumably because I'm doing "Redirect 301 /" which is redirecting the document root of the entire Wordpress multisite instead of the root of the "RewriteCond" domain specified.
How do I set up essentially a conditional redirect based on the domain being accessed?
I should mention that I do not have Nginx installed on the server.
RewriteCond and RewriteRule work together. So the Redirect 301 was running independently of the RewriteCond. This will work:
RewriteCond %{HTTP_HOST} ^(.*)?firstsite.com [NC]
RewriteRule ^(.*)$ https://newsite.com/1234/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)?secondsite.com [NC]
RewriteRule ^(.*)$ https://newsite.com/5678/$1 [R=301,L]
Also, be sure to place this at the very top of your .htaccess rewrite rules. Right underneath the line that says:
RewriteEngine On
Related
I want to redirect all links from my site except the home url to subdirectory.
Example:
www.example.com should remain www.example.com
www.example.com/someurl should redirect to www.example.com/sub/someurl
www.example.com/someurl/anothermore should redirect to www.example.com/sub/someurl/anothermore
Edit: I am currently working on a subdirectory so currently its www.example.com/main/someurl to redirect to www.example.com/main/sub/someurl
To redirect everything to the /sub subdirectory, except the root then you can do something like the following at the top of your .htaccess file in the document root using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule !^(index\.php)?$ /sub%{REQUEST_URI} [R,L]
The above states that if the request is not / (or /index.php) and does not start /sub/ (the RewriteCond directive) then redirect to /sub/<suburl>.
Edit: I am currently working on a subdomain so currently its www.example.com/main/someurl to redirect to www.example.com/main/sub/someurl
That's a subdirectory, not a "subdomain". (?)
If this is the case then move these directives to a /main/.htaccess file and change them to read:
RewriteCond %{REQUEST_URI} !^/main/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/main/sub/
RewriteRule (.*) /main/sub/$1 [R,L]
Basically, i still need the root domain to work in all sub-directories and can't use an apache VirtualHosts redirect because it will invalidate the multisite admin domain, but I don't want users to see the domain without www.
How can this be accomplished?
Redirect plugins won't allow me to redirect from the root domain because they run on the site (w/ www.) rather than the admin root.
I don't want it to redirect all "www."-less urls, only the root.
If you are using Apache 2.4 or newer you could use IF cases within your .htaccess file, test the following:
<If "%{HTTP_HOST} == 'yourdomain.com'">
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
</If>
This should give an 500 server error if you do not have Apache 2.4 installed though, just delete it and your site should work again. I do not know how to solve it for older Apache versions though. I am using this solution for an multisite sub-domain install for 301 redirects, its working pretty good!
I wound up using the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://mysecondsite.com/ [L,R=301]
as it preserves the root domain's subdirectories, allowing you to manage the multisite admin dashboard.
It's almost identical to Panama Jack's answer - honestly I'm not sure what the REQUEST_URI does...
Source: How to redirect root and only root via htaccess?
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.
I am trying to implement an htaccess redirect and it is partially working. It will redirect the root domain http://oldsite.com/ to http://newsite.com/. However, when I try and do more such as http://oldsite.com/eng it will not redirect. Here is my htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newsite.com/$1 [R=301,L]
This is from a wordpress site that had two domains that pointed to the same folder on the server (same wordpress instance). We now want to phase out the old domain so whatever link you go to on http://oldsite.com/ will redirect to the correct part of http://newsite.com/.
You should try and check for the old host before doing the redirect. If it's on the same server it will cause problems without it.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite.com [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
I have multi-site wordpress, I want to redirect the main site to one of the sub-site but redirection in .htacecss gives error. Is there any way to redirect a main site to its sub-site. That looks not possible but I am not a wordpress expert so just verifying.
The redirect should be like
redirect 301 link
Or if there is any other way to do that?
The Redirect directive doesn't take the hostname as part of the URI path (in your case the second argument). You'll need to match that against the %{HTTP_HOST} variable using mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/main-site/sub-site
RewriteRule ^/?main-site(.*)$ /main-site/sub-site$1 [L,R=301]
You'll want to put these above any rules you may already have in your htaccess file.