.htaccess to change the domain in the URL - wordpress

I have the following two WordPress sites.
test.mysite.com
mysite.com
The uploads(images) folder is on mysite.com and I want all image URLs from test.mysite.com to be redirected to mysite.com
For example the link test.mysite.com/wp-content/uploads/2015/09/image.jpg should be redirected to mysite.com/wp-content/uploads/2015/09/image.jpg
I appreciate any help.

If you care about your website traffic and SEO. I would recommend using 301 redirection which is: RedirectMatch permanent
It's quite easy! Make an new .htaccess file at test.mysite.com > Just add this chunk of codes bellow:
RewriteEngine On
RedirectMatch permanent ^/wp-content/uploads/(.*)$ http://mysites.com/wp-content/uploads/$1
Now all images Should be 301 SEO redirected to the concerned location.

You can place this redirect rule just below RewriteBase line:
RewriteCond %{HTTP_HOST} ^test\.(mysite\.com)$ [NC]
RewriteRule ^(wp-content/uploads/.+)$ http://%1/$1 [L,NC,R=301]

Related

htaccess redirect with exceptions

I work on a Wordpress Network. Let's say that the main site has the link www.main-site-old.com, and all the other sites in this network have links like www.main-site-old.com/site1 , www.main-site-old.com/site2 e.t.c.
I want to redirect ONLY the main site to a new domain (let's say www.main-site-new.com) and let all the others stay as they are.
I tried this one:
Redirect 301 / http://www.main-site-new.com/
but it redirects the whole domain (for example it redirects www.main-site-old.com/site1 to www.main-site-new.com/site1)
I also tried:
RewriteCond %{REQUEST_URI} !=/site1
RewriteRule ^.*$ http://main-site-new.com/$0 [R=301,L]
but nothing happened.
With mod_alias redirect you will not be able to use regex to capture a request for http://www.main-site-old.com only so, you could use another one RedirectMatch like this :
RedirectMatch 301 ^/?$ http://www.main-site-new.com
Or go to mod_rewrite by this :
RewriteEngine On
RewriteRule ^/?$ http://www.main-site-new.com [R=301,L]
Note: clear browser cache then test.

301 Redirect not working (WordPress) - [ Probler with ?m=1 in URL ]

I have very weird problem.
My WordPress site gets traffic to the same page with different additional parameters.
Example:
mysite.com/page1.html?m=1
mysite.com/page1.html
It is the same page, but ?m=1 makes WP show 404 error page.
I tried 301 redirect like this (in actual HTACCESS file i also use http:// but here I cannot):
Redirect 301 /page1.html?m=1 mysite.com/page1.html
But this does not do anything.
Traffic comes from google, so I cannot change this URL structure - I have to work with what I got ... So how I fix this???
It could be either WP or HTACCESS problem ... I searched and cannot find anything - i get results for M1 rifle :(
Please help - this is a live site
You can't match against the query string in mod_alias's Redirect or RedirectMatch. You need to use mod_rewrite's %{QUERY_STRING} or %{THE_REQUEST} variables. Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [L,R=301]
This will redirect any request that has the "m=1" query string and remove it.
.htaccess
RewriteEngine On
RedirectMatch 301 ^/page1.html?m=1$ /page1.html
Try that see also this answer .htaccess 301 redirect of single page

Using .htaccess to redirect a domain to another URL

I have a site running wordpress, it's the full site. One of the pages is like a contact-us form located at www.ourdomain.com/contact-us/
I also have a URL like contactourdomain.com and I want it to redirect to www.ourdomain.com/contact-us/
We used to do this with a redirect on network solutions, but I prefer to have it all done right on the server if possible. I've got it sort of working but when you visit the link is still says contactourdomain.com/contact-us/ as the URL, and that breaks all the other ones.
Any suggestions?
.htaccess
Options -MultiViews
RewriteEngine on
RewriteBase /
# Rewrite
RewriteRule ^(.*)$ http://www.ourdomain.com/contact-us//$1 [L]
if you add this in .htaccess is it working?:
RewriteRule ^$ contact-us/ [R=301,L]
keep me posted..

.htaccess Subfolder Redirect

I recently moved my WordPress website to a subfolder. I want to add a redirect in the .htaccess file so that the links to images I've uploaded originally to ~/wp-content/uploads/ will pass to ~/blog/wp-content/uploads. The .htaccess file must remain in the root folder for WordPress to read it properly. This is what I tried
Redirect /wp-content/uploads/ /blog/wp-content/uploads
That worked great, except I host other domains on this account, and all of the other domain's upload folders were being redirected in a similar manner.
Is there a way to restrict this redirect to just one domain? So that example.com/wp-content/uploads redirects to example.com/blog/wp-content/uploads, but another.com/wp-content/uploads does not?
Thanks everyone!
Assuming you want a 301 redirect, using this RewriteEngine example should work:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/blog/wp-content/uploads/$1 [R=301,L]
You should place the Redirect inside the <VirtualHost> definition for example.com in the httpd.conf (or equivalent) instead of .htaccess
on a sidenote, Redirectsays temporary / 302 by default, so it is nicer to use
Redirect permanent /wp-content/uploads/ /blog/wp-content/uploads instead

301 Redirect & site alias

so I have a site I want to take offline. developmentsite.com and I want to redirect to a new site.
So I do a 301 redirect.
Redirect 301 / http://www.newssite.con. Now I want to continue working on developmentsite.com so I set it up as an alias on my own site
mysite.developmentsite.com but because the .htaccess in the main httpdocs folder both sites redirect.
How can I stop the alias redirecting?
Thanks,
Ross
You need mod_rewrite for this.
RewriteEngine On
RewriteBase /
# exclude the dev site
RewriteCond %{HTTP_HOST} !^mysite\.developmentsite\.com$
RewriteRule ^ http://www.newssite.com/ [L,R=301]

Resources