We have two domains on two different hosts (both wordpress installations):
oldsite.com
newsite.com
A lot of articles were mirrored on a new site with the same title but a different url structure:
eg:
oldsite.com/posts/article-about-cats
newsite.com/news/article-about-cats
another scenario:
oldsite.com/posts/article-about-cats
newsite.com/news/2018/08/09/article-about-cats
Assuming the title of the article is the same, what would a rewrite rule(s) be in .htaccess on the oldsite server?
As mentioned in my comment above that you cannot have generic rule for second scenario where target URL has dynamic part such as /2018/08/09/, which is not part of original URL.
For the first scenario, you can use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldsite\.com$ [NC]
RewriteRule ^posts/(.*)$ http://newsite.com/news/$1 [L,NC,NE,R=301]
Related
I would like to redirect all users who visit pages on my old domain to the same page on my new domain (only the domain name is changing).
If my old domain is www.OLDDOMAIN.com and my new domain is www.NEWDOMAIN.com then I want to be able to redirect visitors who visit www.OLDDOMAIN.com/c/123/Page-Name to www.NEWDOMAIN.com/c/123/Page-Name (note that these are SEO friendly URLs and not simply sub directories or filenames)
However, I do not want this rule to apply to visitors from a certain IP addresses (lets say 2 IP address 123.123.123.123 and 345.345.345.345 - I appreciate these aren't valid).
I would also like to ensure that all subdomains e.g. files.OLDDOMAIN.com are NOT redirected to files.NEWDOMAIN.com
AND I would like the ability to add exclusions to this rule. E.g. I want to continue to be able to access www.OLDDOMAIN.com/index.php or a subfolder e.g. www.OLDDOMAIN.COM/admin/
The reason for need to do this is the website currently on www.OLDDOMAIN.com is being moved to www.NEWDOMAIN.com. and www.OLDDOMAIN.com is being repurposed for another website. The reason I need the redirects is to retain search engine rankings, at least until the new site is established. The solution therefore needs to be SEO friendly.
Apologies in advance for not supplying any code but I'm not entirely sure where to start with this one...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# exclude these IPs
RewriteCond %{REMOTE_ADDR} !^(123\.123\.123\.123|345\.345\.345\.345)$
# exclude these URIs
RewriteCond %{REQUEST_URI} !^/(/index\.php|admin/) [NC]
# exclude sub-domains
RewriteCond %{HTTP_HOST} ^(www\.)?OLDDOMAIN\.com$ [NC]
# redirect to new host
RewriteRule ^ http://www.NEWDOMAIN.com%{REQUEST_URI} [R=302,L,NE]
Upon request I have reformulated my question for clarity so others might benefit better from the answers.
I have 3 domains for the company I work for:
bizwizprint.com (the main website that is hosted on a shared server)
bizwizsigns.com (secondary domain with no hosting attached)
boatwiz.com (tertiary domain with no hosting attached)
The goal is to get my second and third domains to redirect to the first domain onto their own respective landing pages.
First Step: At the domain registrar, change the DNS "A Records" of the second and third domains to resolve to the same IP address that the main website is hosted on.
Second Step: Create a "Site Alias" on the main website server for the second and third domains, they will point to the root directory where the main website files reside.
Third Step: Create or edit an .htaccess file that will redirect the requests for the second and third domains and point them to the landing pages that I have created for them.
The question: What rules do I add to htaccess?
Essentially, I would like to have a user type in "boatwiz.com" in the address bar and the browser will literally GO TO "bizwizprint.com/boatwiz.html".
Please note: I do not want any rewrite rules that will change the actual URL to boatwiz.
The reason for this is that it is a temporary thing. Eventually there will be an actual "boatwiz" website and "bizwizsigns" website and they will most likely be very different in structure. I don't want it to appear that I have three domains with all the same content, or have people make any bookmarks that I will need to redirect yet again in the future.
"How do I redirect an external domain (boatwiz.com) to land in a specific page of a new domain (bizwizprint.com/boatwiz.html) without any rewriting?"
So you probably mean that you want an "internal redirect", not the "external redirect", right? I.e. you want e.g. the bizwizsigns.com to stay displayed in the browser Location bar, but show the contents
of bizwizprint.com/signs, right?
1. htaccess only - impossible
Well, using only .htaccess, this is impossible, because the different domain will force the external redirect. Citing the docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as a
URL-path. Otherwise, an external redirect is performed for the given
URL. To force an external redirect back to the current host, see the
[R] flag below.
2. Iframe trick
What you could do is to use iframe. Put this code at bizwizsigns.com/index.html:
<iframe src="http://bizwizprint.com/signs" width="100%" height="100%"
style="border: 0 none;" frameborder="0">
But there are many downsides of this solution:
the URL will not change in the browser's location bar, even if user clicks within the iframe
browser bookmarks and history will not work as expected.
3. Clever hosting setup (domain aliases into the same dir)
Are you in a hosting environment, or do you have your own server? Sometimes the hosting allows you to make aliases of several domains that are handled by the same local directory tree. In that case, you won - you can write .htaccess so that it handles the requests as internal redirects:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ /signs [L]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ /boats [L]
which will (internally) redirect bizwizsigns.com to /signs (= your content of bizwizprint.com/signs, because you have one hosting server directory for all 3 domains). But if you e.g. want all queries like bizwizsigns.com/<foo> to be redirected to bizwizprint.com/signs/<foo>, you have to be more careful - see the added condition on REQUEST_URI to prevent endless loop:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/signs/ [NC]
RewriteRule ^.*$ /signs/$1 [L]
Assuming that you have all 3 domains pointing to the same document root, you just need this in its htaccess file:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/signs [L,R=301]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/boats [L,R=301]
RewriteCond %{HTTP_HOST} (bizwizsigns|boatwiz)\.com$ [NC]
RewriteRule ^(.+)$ http://bizwizprint.com/$1 [L,R=301]
So if it's just http://bizwizsigns.com/ or http://boatwiz.com/, then you get redirected to http://bizwizprint.com/signs or http://bizwizprint.com/boats. But if you have anything after the last /, like http://bizwizsigns.com/foo/bar.html then you'll get redirected to http://bizwizprint.com/foo/bar.html.
Here is what I'm trying to do.
I have 2 apps running on my site one at root / and one at at /blog/.
I want to be able to access some pages that are served by the blog app but without the /blog/ part in the URL. This bit I have managed to do with the following rule.
RewriteRule my-page blog/index\.php?page_id=1 [L]
This allows me to view a page actually at /blog/my-page at /my-page.
Now where I'm struggling is with ensuring there are no duplicate URLs, so I'm trying to redirect to my shorter URL. Like so:
From:
/blog/my-page
To:
/my-page
From:
/blog/index.php?page_id=1
To:
/my-page
With the following rule, I can redirect from /index.php?page_id=1 to /my-page
RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule index\.php$ my-page? [R=301,L]
After this rule happens the first rule I mentioned takes it to the right place.
My question is:
How can I get it to work with blog as part of the URL. I expected the below to work but it doesn't
#RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule ^blog/index\.php$ my-page? [R=301,L]
The reason this was not working was because there was an .htaccess file within my /blog/ directory.
This included a rule that would catch anything and route to blog/index.php.
Moving my rules to /blog/.htaccess allowed them to function properly
So I've been looking but so far can;t find something that I'm looking for exactly. Looked at this article How can I forward ALL pages to the same exact page on a different domain using .htaccess? but it seems to only redirect my home page.
Currently, I'm using WordPress and I need to be able to forward all pages to a new domain. For example, domain1.com/about-us needs to go to domain2.com/about-us. But I have about 50 pages this needs to work on. I would like to see if there is a 1-5 line code to use for this to work.
Thanks!
Try putting this (above any wordpress rules) in the htaccess file in domain1.com's document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
If your 2 domains are one different webservers, or don't share a common document root, you can just use mod_alias, adding this to the htaccess file in domain1.com's document root:
Redirect 301 / http://domain2.com/
I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.