Redirecting a WP multisite URL to a subsite URL - wordpress

I have a WordPress multisite installation with a base URL of https://example.com. Subsites have an initial URL of https://example.com/forinstance, which can be changed in WordPress to an alias of https://forinstance.com.
But I want to redirect all URLs from https://example.com/caseinpoint ALSO to https://forinstance.com. There's not a way to do that in WordPress, so I'm trying to do it in my .htaccess file. I can't get it to work though.
I've tried variants of:
RewriteCond %{HTTPS_HOST} ^example.com/caseinpoint$
RewriteRule (.*)$ https://forinstance.com [R=301,L]
But it always just goes to a 404 on example.com.
However, this DOES work for other subsites:
RewriteCond %{HTTPS_HOST} ^ejemplo.com$
RewriteRule (.*)$ https://perinstanza.com [R=301,L]
So it seems to be something to do with the multisite. How can I redirect example.com/forinstance, please?!

Neither of the rules you posted will actually do anything since HTTPS_HOST is not a valid server variable. It should be HTTP_HOST, which represents the Host HTTP request header (hence the HTTP_ prefix). ie. the hostname (or domain) being requested.
The Host header does not include the URL-path, which you are trying to match in the first rule.
Presumably you want to redirect the remaining URL-path to the same on the new domain. eg. https://example.com/caseinpoint/<url> should redirect to https://forinstance.com/<url>? You aren't literally just redirecting https://example.com/caseinpoint (missing trailing slash) to https://forinstance.com/ (there is always a slash after the hostname) as you've stated?
In which case you can do something like the following at the top of the root .htaccess file, before any existing WordPress directives.
RewriteCond %{HTTP_HOST} ^(www\.)example\.com [NC]
RewriteRule ^caseinpoint(?:$|/(.*)) https://forinstance/$1 [R=302,L]
This also allows for an optional www subdomain.
The $1 backreference contains the value of the first capturing subpattern in the RewriteRule pattern, ie. the URL-path after "caseinpoint/" (not including the slash).
Test first with a 302 (temporary) redirect to avoid any potential caching issues. Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed it works as intended. You will likely need to clear your browser cache before testing.

Related

Editing htaccess file to redirect only content + Wordpress

I have created a subfolder in my webserver and installed wordpress there, it's working ok. And now i have created several subdomains. I need to show pages made in the wordpress installation in those subdomains but without changing the addess shown in the web browser address bar.
Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/WPweb/index.php/pagetobeshown [R=301,NC,L,QSA]
with this i am being redirected and content is shown ok, but the address bar is changed.
is it possible to fix this so that address is not changed?
thanks in advance
To do this in .htaccess you would need to configure your server as a reverse proxy and proxy the request from the subdomain to the URL on the main domain. WordPress will then see this as a request for the main domain.
However, this does require some configuration in your server config to ensure the appropriate modules are loaded (mod_proxy, mod_proxy_http, etc.) and optionally to set ProxyPassReverse if WordPress should issue any redirects.
You can then use the P flag with mod_rewrite's RewriteRule to send the request through mod_proxy. For example:
RewriteCond %{HTTP_HOST} ^sub\.example\.com
RewriteRule ^$ https://example.com/WPweb/index.php/pagetobeshown [P]
This sends requests for https://sub.example.com/ only (ie. the document root of the subdomain) to the URL as stated. The NC, QSA and L flags are not required.
If you want to send any URL-path (as implied by your RewriteRule directive) to the URL on the target site then change the RewriteRule pattern from ^$ to ^ (simply remove the $). However, this many-to-one relationship is not necessarily good for SEO.
You will still need to modify the rel="canonical" link elements in the page to the appropriate URL in WordPress itself.
However, I'm sure there is a better, more WordPress-specific way to solve this. But WordPress would need to be configured to accept requests to the subdomain and the alternative URL-path.

Redirect custom urls to root domain using htaccess

Having a wordpress website, I have a list of urls that I would like to have redirected to the root domain using the .htaccess file. Nothing fancy here just that when looking for this info I either found how to redirect all urls or specific folders but not a custom list of urls.
For example one of my urls is like this: example.com/path1/path2/ and I can easily add Redirect 301 /path1/path2/ http://example.com/ just under the RewriteEngine On line in my .htaccess file.
By doing this, any other example.com/path1/path2/moreUrlPath/here will be redirected to example.com/moreUrlPath/here and adding another custom redirection line like this Redirect 301 /path1/path2/moreUrlPath/here http://example.com/ will have no effect whatsoever.
Bottomline, having a list of urls, how can I permanently redirect them to the root domain?
Use mod_rewrite and a RewriteRule instead of Redirect.
The RewriteRule uses a regular expression to match the URL, so you can say
RewriteRule ^path1/path2/$ http://example.com/ [R=301,L]
to match exactly "/path1/path2/" (you are always beneath / when working in the .htaccess, so the leading / isn't included). ^ stands for "start matching at the beginning" and $ means "and stop at the end", so there can't be anything after path2/. This allows you to be very specific, e.g. you could go for
RewriteRule ^path1/path2/a$ http://example.com/ [R=301,L]
RewriteRule ^path1/path2/aa$ http://subdomain.example.com/ [R=301,L]
When you don't need to be specific, and you want to redirect everything under a certain path, you use an open ended match:
RewriteRule ^path1/path2/ http://example.com/ [R=301,L]
This will redirect everything beneath /path1/path2/ to the root directory, and it will NOT care about what comes after path2/, so /path1/path2/test/ will not redirect to http://example.com/test/ as it would with Redirect.
You can do that too, though, by using backreferences and matching certain parts with parentheses, e.g.
RewriteRule ^path1/path2/(.*) http://example.com/$1 [R=301,L]
Which will then redirect /path1/path2/ to http://example.com/, but /path1/path2/test/ to http://example.com/test/.

Redirect subdomain and path to main domain with same path

I have recently stopped using a subdomain for my blog, i need to forward all links that use that domain to the same link path but on my main domain.
Example being:
blog.example.com/blog/sales/blog-title
needs to redirect to :
www.example.com/blog/sales/blog-title
I can redirect the subdomain itself so
-blog.example.com/
goes to
www.example.com/blog
but as soon as I try to redirect a page it doesn't redirect and doesn't load. Can anyone shed some light on it? I currently have this in my htaccess for my main redirect:
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule (.*) http://www.example.com/blog/$1 [L,R=301]
I have other subdomain redirects going on but none wildcard redirect.
In your example, you want to redirect from
blog.example.com/blog/sales/blog-title
to
www.example.com/blog/sales/blog-title
But in your rule, you insert another subdirectory blog in the substitution part, which gives
www.example.com/blog/blog/sales/blog-title
instead, and an error 404 as a result.
To redirect from one domain to another with the exact same request path, use
RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R]
When it works as it should, you may replace R with R=301. Never test with R=301.

redirect a domain extension to a subdirectory

I am trying to create a permanent htaccess redirect (301) from all my domain extensions into the appropriate subdirectories. The "rules" are as follow:
Redirect belgian website to its subdirectory on the main website:
from: www.example.be
to: www.example.com/befr/
Of course I would like to preserve the url parameters (if any) of the "from". Globaly, if someone entered the first url it should redirect to the second url (langage subdirectory in the main website).
I'm using wordpress and I'm hosting on a plesk I've read many things here but I'm stuck, thank you very much in advance for your help
PS: I've tried that but it doesn't work
RewriteCond %{HTTP_HOST} ^(www\.)?example.be$ [NC]
RewriteRule ^(.*) http://www.example.com/befr/$1 [L,R]
After reading your question, your code should be working (with informations you gave).
If it's not, here are some points to check:
1. Make sure mod_rewrite is enabled and htaccess can be executed (Apache config).
2. Your htaccess has to be in root folder (where example.be is forwarded).
3. About your htaccess' code:
since you're using Wordpress, make sure your rule is on top of other rules
don't forget to escape . (second one) in RewriteCond (otherwise it doesn't mean the same) even if it works that way
replace R flag (302 by default) by R=301 if you want a 301 redirect
Your code now looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.be$ [NC]
RewriteRule ^(.*)$ http://www.example.com/befr/$1 [R=301,L]
# your other rules (and Wordpress' default rule) here

How do I modify my .htaccess file to redirect a URL for one domain only, and not rewrite other domains hosted by the same server

I switched blogging software (MT to WordPress) on a site and need to redirect requests to http://www.domain1/atom.xml to http://www.domain1.com/feed/atom.
I was using a simple Redirectmatch rule, but realized that it was also redirecting requests made to another site (domain2), that is is hosted by the server, in a subdirectory of domain1, which I do not want to happen (its feed is still at http://www.domain2.com/atom.xml).
How do I get the redirect to only occur for domain1?
I was trying to do the following, but it didn't work.
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^/atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
Am I close?
Thanks,
Rich
If you don't do any rewriting for domain2 then a quick fix would be to create a .htaccess file inside its root folder and disable rewriting with RewriteEngine off.
Otherwise you are on the right path with the RewriteCond, it should do the trick. Have you tried adding $ at the end (RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]) / any misspelling / www. vs no www.?
I figured it out, but I'm not sure why exactly this works. I moved my .htaccess to be:
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
I removed the slash in front of "atom" in the RewriteRule.
I would think I should have the slash, as I'm tyring to redirect http://www.domain1.com/atom.xml .
It's at the root of the domain...
Oh well. Can anyone explain why this works? Is the string passed to the pattern matching not contain the starting slash?
Thanks,
Rich

Resources