Htaccess Redirect All To Index Php
Redirecting pages or websites tends not to be something almost spider web designers deal with on a regular basis.
What sometimes happens is you change your domain and go along the pages the same on the new domain.
Yous don't want to redirect all the pages of the old domain to the homepage of the new domain, you lot want them to go to the right internal pages.
I've had this exact question from 3 unlike people in the concluding week, then I figured it deserved a tutorial.
If you're just trying to redirect from the http version to https version of the same domain or the www to non-www (or vice versa), I have a tutorial on redirecting to the same domain.
How to redirect all pages to another domain using .htaccess:
- Set up the nameservers of the old domain to somewhere you have cPanel hosting
- Get into cPanel and add together the old domain
- Add a .htaccess file that 301 redirects all the internal pages to the new domain
- Add together a blank index.php for extra prophylactic.
If yous already know the steps and are just looking for the htaccess code, it is:
RewriteEngine on RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L] In this example, I have a domain, inteliwolf.com (notice the single "L") that I desire to redirect to intelliwolf.com (the site you lot're reading this on, with the two "L"s).
Inteliwolf.com is registered at GoDaddy and I have some free space on a HostGator host that I'll use.
The steps are the same, no matter which registrar and host y'all use. The screens will merely look a chip different.
At your domain registrar, become into the DNS Manager for the erstwhile domain.
You lot tin attempt Domain Forwarding if that works. I've found this takes ages to actually redirect and it has issues with redirecting internal pages.
We're non going to forwards the domain in this tutorial. This section is but in case it works for yous.
If you did want to utilize Domain Forwarding, you lot put your destination domain in the Forrad To box and select "https://".
Cheque Update my nameservers and DNS settings to support this change and get out everything else as default.
Click save, and wait for a few hours for the DNS to propagate.
If this works for you, y'all can stop the tutorial now.
We'll proceed with the tutorial every bit if you haven't done the domain forwarding.
Point the nameservers of the old domain to your host
You might take space to add an addon domain at the same host y'all utilize for the destination domain. That's typically where I prefer to host this redirect, just to proceed things in one spot.
It's perfectly fine for you to host the old domain where information technology was or at another host.
The main thing is it has cPanel. If it's a Windows IIS server, Parallels or something else, the implementation will be different from what's shown here.
In DNS Direction, click "Change" next to the nameservers.
Switch from Default nameservers to Custom nameservers.
When y'all setup the host, you would've received an e-mail listing the two or more nameservers for that hosting account.
If you can't find the email, you might be able to find it in the sidebar of your cPanel. It will probably be listed under Primary and Secondary Nameservers.
For the purpose of this tutorial, I'll be using
ns8439.hostgator.com ns8440.hostgator.com There will usually be two nameservers, sometimes three or more.
Enter the nameservers at the domain registrar as shown and click "Save".
Add the old domain to the host
While the DNS is propagating, get to where you'll be hosting the erstwhile domain.
Assuming it's not already setup at that host, and that it's not the primary domain, set up it upwardly as an Addon Domain by going to Addon Domains.
Enter the old domain in New Domain Name. Everything else should exist fine equally default.
There's no reason to create a new FTP business relationship for this domain.
Click "Add Domain".
If everything's good, you lot'll see a success message.
Add together the .htaccess file to the old domain
Now to add the code that will actually do the redirecting.
Go to File Manager.
Navigate to the folder yous added for the old domain in the Addon Domains step.
HostGator already had a .htaccess file in the folder, so yous just select information technology and click Edit.
If you don't have a .htaccess file in that location, first bank check you tin can run across hidden files, by going to Settings (in File Director) and checking Show Hidden Files (dotfiles).
If y'all still tin't see a .htaccess file in the abode binder for the domain, click "+ File" and create one.
In .htaccess, add the following code:
RewriteEngine on RewriteRule ^(.*)$ https://world wide web.yourdomain.com/$1 [R=301,50] The key line is RewriteRule.
The line says using ^(.*)$ "take the whole page, not including the domain..."
"... and stick it where the $1 is".
So if someone goes to https://quondam.com/important-page/ the code will accept important-folio/ and tack it on to the end of https://www.newdomain.com/ so the visitor ends up on https://www.newdomain.com/of import-page/
Be certain to change https://www.newdomain.com in the code to your new domain.
As you can see, I've changed mine to intelliwolf.com.
Click "Salvage Changes".
Add together bare index.php file
For an added layer of security, I don't like to exit a domain empty similar this.
You lot should always add a file like index.html or index.php even if it's blank.
When you do that, if something goes incorrect with the .htaccess redirect and it doesn't end up redirecting, anyone going to the former domain will just encounter a bare page, rather than a listing of all the files in there.
Click "+ File" to add together a new file.
Telephone call the new file alphabetize.php and click "Create New File".
You don't accept to add annihilation into this file. If everything goes well, no one will always see it.
It may take a few hours for the redirect to get-go working.
The domain registrar has to tell all the other DNS phonebooks that your onetime domain is now located at the new host.
Once that'due south washed, you probably won't need to touch it again. There'southward nada to update and this code has worked for the 20-odd years I've been building websites.
How to redirect a specific page to some other domain with .htaccess
If you desire to redirect just one page from the old domain to somewhere different, you lot just need one line:
Redirect 301 /quondam-url/ http://newdomain.com/new-url/ Replace /old-url/ with any comes afterward the domain for the URL you want to redirect from.
Then put the full URL of the destination page.
You can use this independently of the domain redirect I showed at the start.
If, instead of redirecting the entire site, you just wanted to redirect ane URL from the old domain (maybe information technology was on your business cards), then you could merely put this line in the .htaccess instead of the RewriteRule line.
Source: https://www.intelliwolf.com/redirect-all-pages-domain-to-another-with-htaccess/
Posted by: johnsoncontly.blogspot.com

0 Response to "Htaccess Redirect All To Index Php"
Post a Comment