Redirect Specific Pages Then Redirect All Others - .htaccess

Where to start.
I need to move a website to a new subdomain with around 20 specific pages, and then just blank redirect everything else.
Any ideas?

You'll want something like this in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# Redirect specific pages
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(index\.html)$ http://newdomain.com/path/$1 [R=301,L,NC]
RewriteRule ^(index2\.html)$ http://newdomain.com/path/$1 [R=301,L,NC]
RewriteRule ^(index3\.html)$ http://newdomain.com/path/$1 [R=301,L,NC]
# Redirect all others to the new domain
RewriteRule ^(.*)$ http://newdomain.com/ [R=301,L]
Customize and test for your situation using: http://htaccess.madewithlove.be/

Related

301 redirection whole domain to new one + few individual pages to new urls

I have olddomain.com - and I want to redirect to newdomain.com with htaccess and 301 - thats easy and working very well for me - if I am redirecting whole domain.
But on the new domain I changed few urls (now they are different then on the previous domain) and I want to redirect whole domain and few specific pages to few specific pages and I dont know how to combine this 2 conditions (redirect whole domain and redirect few specific pages).
This is working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]
and I would like to add to the code some specific redirects like this but I dont know how to combine it together that it will be working:
Redirect 301 /something/ https://newdomain.com/something-changed-new/
Thank you in advance for a help.
Check this rewrites in top of your .htaccess file
RewriteEngine On
RewriteRule ^something\/$ https://newdomain.com/something-changed-new/ [R=301,L]
RewriteRule ^other\/$ https://newdomain.com/something-changed-other/ [R=301,L]
RewriteRule ^old\/$ https://newdomain.com/new/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://newdomain.com/$1 [L]

Rewrite only one specific url

I want to rewrite one specific url.
http://example1.com should be http://example2.de .
But http://example1.com/subdir or http://sub.example1.com should remain the same.
I found the following, which successfully rewrites example1.com, but also every url which starts with example1.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Background: I want to redirect the main page of an WP-Multisite but want to make sure that I can work with the backend of wordpress and run other multisites which are subdomains.
For matching only http://example.com domain (without possibility to add anything before or after the example.com) use the following code:
RewriteCond %{HTTP_HOST} ^(example.com(\/{0,1})){1}$
RewriteRule http://example2.de(\/{0,1}) [R=301,L]
That (\/{0,1}) part is for matching both example.com and example.com/ (but nothing esle) - if you do not wish to match example.com/ remove that part from both rows.
You're pretty close but you don't need to capture URI in $1:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^$ http://example2.de/ [L,R=301]

Redirect root (and only root) URL to another domain?

I'm looking to redirect my domain example.com and www.example.com to http://example2.com, but I want to keep all visits to my posts at http://example.com/sdfhs to continue through to their destinations.
Essentially, I ONLY want to redirect the root and www domain and leave the rest untouched.
How can I do this with .htaccess?
Don't know if you still have this issue, but I have had it on one of my domains (Yourls installation). Try:
RewriteEngine On
RewriteRule ^$ http://www.example.com/ [R=301,L]
It works on my installation, but check it out on your site.
Why not
RewriteEngine On
RewriteCond ${HTTP_HOST} example\.com|www\.example\.com
RewriteRule ^/?$ http://example2.com [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond ${HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://example2.com [L,R=301]
You could simply match any URL with at least 1 character (.+) and mark that as the last rule for the rewrite engine to look at [L].
Then afterwards redirect anything else (.*) to the new domain (the only other thing that wouldn't be matched up to that point would be the root).
RewriteEngine On
RewriteRule ^(.+)$ - [L]
RewriteRule ^(.*)$ http://example2.com/$1 [R=301,L]

Problem 301 redirect not allowing login in the backend

I am finding some problems in the htaccess of CMS with a 301 redirect.
When trying to solve canonical urls (redirecting site to www.site) I got the problem that I cannot log in in the back end (www.site/admin).
The htaccess condition is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.site\.co.uk$
RewriteRule (.*) http://www.site.co.uk$1 [R=301,L]
I guess I need to include a expression that allows the URI /admin not to be redirected, but how?
Like this, for example:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.co.uk
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule .* http://www.example.co.uk%{REQUEST_URI} [R=301,L]

Use htaccess to redirect all traffic from root to specific subdomain page

I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]

Resources