Automatically change domain name in URLs - .htaccess

I'm creating a mirror for my website and I need to automatically change domain name in URLs on my new website, using some .htaccess command on my new site.
What I need is to change all links from www.old.com/any_link.html to www.new.com/any_link.html for all users on www.new.com without changing anything in database (having the same database for both sites). So that I get two independent websites www.old.com and www.new.com working at the same time.
I know about redirect 301 from old site to the new one, but I need redirecting INSIDE new website, without changing anything on the old one.
Is that possible at all?

try writing this on .htaccess :
RewriteEngine on
RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,L]

Related

Fake subdomain for different users while still browsing same folder structure

I'd like to create fake subdomains for different users for more vanity, and to make the user (in this case a company) feel they are in a more isolated environment.
For the sake of maintainability, it's important for me that all users still browse the same files, to avoid having to update files for every single user that exists when updating the code.
My website has one public part at root, let's say www.example.com. I'd like to be able to fake the following kind of subdomains:
user1.example.com
The true URL would be www.example.com/member/?user=user1. I'd like for the folder structure to follow the same pattern. www.example.com/member/settings/?user=user1 would appear as user1.example.com/settings/ and so on.
I assume this would best be achieved with .htaccess, no?.
What is the proper .htaccess code for this?
Thank you!
based on the information you've provided here's a bit of .htaccess not tested but might do the job.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=(.*)$
RewriteRule ^(.*)$ http://%1.example.com/$2? [R=302,L]
Once you find it working fine, replace 302 to 301
Note: It's important to note that you'll need to add a subdomain wildcard
In the end, this was super easy to solve! It wasn't solved the way I first expected though, using .htaccess. I solved it with something called wildcard subdomain.
When you register a new subdomain, enter * in the domain prefix, such as *.example.com. A folder for the wildcard subdomain will be created on your server, such as _wildcard_.example.com. Whenever you access site1.example.com, fakesub.example.com etc, the browser of the visitor will read the files in the wildcard.example.com folder.
The beauty of it all is that if I create a certain subdomain that I want to use, for example forum.example.com, this real subdomain will have priority over the wildcard subdomain, and files will be fetched from the folder for this subdomain, as opposed to from the wildcard subdomain folder.
I use PHP and need to know the subdomain to fetch the appropriate database for the current user. To do this, I use the following code:
$subdomain = explode('.', $_SERVER['HTTP_HOST'])[0]
With a wildcard SSL cert I have all of these subdomains secure.

How to redirect entire domain to new domain except homepage via htaccess

So I have found some questions that are close to what I need, but I haven't found exactly what I'm looking for.
While I've worked with htaccess files in the past, it's pretty much been just copying and pasting code that is proven to work, or just doing simple 301 redirects. To be honest, I get pretty confused when looking at htaccess rewrite rules, so unfortunately are not going to be able to do from scratch. So I'm really hoping there is someone kind enough to provide a code example just using placeholder domain names.
Here is my situation. I'm doing work for a company who has split up into different divisions (4). So they want to take the site on their current domain, move it to a different domain. Then put up a simple html splash page on the old domain that provides links to the 4 different sites they will have (one of those being what was the old site on a new domain).
So I need to redirect everything from olddomain.com to newdomain.com with the exception of the homepage. Not sure if this matters, but site that was on the old domain was a WordPress site so technically is index.php. But the new site/splash page is named index.html.
Thanks in advance!
So I need to redirect everything from olddomain.com to newdomain.com with the exception of the homepage.
You can use this rule in the site root .htaccess of olddomain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/(css|js|images)/ [NC]
RewriteRule . http://newdomain.com%{REQUEST_URI} [L,NE,R=301]
RewriteRule . will match everything except the home page just make sure this rule is the first rule in your .htaccess.

Shall i implement the 301-redirection method to my site inner pages?

Actually I am working in a one url, now my client told me to change the domain name, so I have planned to do 301-redirection for my home page in a new domain. But i have worked for both home and inner pages on my previous site. Is it recommended to do the 301-redirection for my inner pages too, please help me....
There should be an option in your hosting panel to manage redirects you can then redirect the whole domain, and send it to the new domain. I hope this helps.
Yes, it is recommended that you redirect all of your existing pages/urls to new domain. This will retain your SEO ranking and also the customers who've bookmarked earlier domain.
However, instead of doing a redirect of each individual URL, you could use the power of .htaccess file and put below code to catch all the request coming for old domain (lets say example.com) and redirect them to corresponding pages on new domain (lets say example.org)
You could use something like below
# Detect if the request was raised for old domain
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
Hope it will help.

What to do with dynamic url after rewrite?

After you've been able to successfully create a url rewrite how do you handle the original and other possibly ways to access a page. This of course to prevent duplicate content. For example if I have this:
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(\d+)/([\w\-/\.]+)/?$ blog.php?id=$1&article_title=$2 [L]
I'm able to access the page by the url
https://www.mysite.com/blog/10/mysite.com (the mysite.com is the article title)
The problem is I'm also able to access the site by going to
https://www.mysite.com/blog.php?id=10article_title=sitetitle
https://www.mysite.com/blog.php?id=10
ect.
How are you supposed to handle those particular urls.
Also should I change the blog.php?id=10 to the rewritten url? Can I rely on something else and just start using the full rewritten url now? The site is new.
For my web site, I have the script that gets called from inside the rewrite detect the URI they were fetched from (using the "REQUEST_URI" variable that at least Apache sets), and redirect to the canonical one if they ever get called with the internal one (outputting a 301 direction).

How does google react on a redirect to a redirect

I am migrating a website to a drupal CMS.
I would like to know how google responds on a redirect from a redirect.
I'll try to explain what i'm trying to accomplish:
The old situation: www.old-site.com/page?pid=123
The new situation: www.new-site.com/page/pagetitle
I want to create a 301 redirect in my .htaccess file as i have done for alot of other pages.
But in this particular situation the old page id (pid) is not known in the new website.
My plan is to create the following rewrite rules:
RewriteCond %{QUERY_STRING} ^pid=(.*)$
RewriteRule ^page.aspx$ page-redirect/%1? [R=301,L,NC]
Right now i have the correct page id. What i then want to do is create a little script that rewrites the url from page-redirect/123 to page-redirect/newtitle as i do know the title that comes with the id
Then i want to have the following rule handle the new title:
RewriteRule ^page-redirect/(.*)$ page/%1? [R=301,L,NC]
So basicly the bottomline question is: How does this affect the google index? Our client wants to maintain his (good) organic results in google.
Instead of Redirecting from page.aspx to page-redirect, why not Rewrite to page-redirect. That way page-redirect can figure out the final url page-redirect/newtitle and issue a single 301 redirect for it i.e.
RewriteCond %{QUERY_STRING} ^pid=(.*)$
#just do a server side rewrite instead of a redirect
RewriteRule ^page.aspx$ page-redirect/%1? [L,NC]
The redirect logic will exist within your page-redirect code.
If you still choose to do 2 301 redirects, it is not ideal, but Google should transfer the page-rank of the old urls to the new ones.
Note that either way, when you do move content via a 301 (1 or 2), you should expect a temporary dip in your page rank as your old pages drop off the index and the new ones are added.

Resources