htaccess 301 redirect url - php

I have a small dilemma.
I have been working on a project for almost a year now and had an old domain name. Not knowing how Google has indexed the whole site under the old domain which has worried me. I want to put the site up on my new domain name and I think doing a 301 is the way forward. I will need to do this on every dynamic page. The good thing is the page structure is identical.
Any advice on the best way to do this would be massively appreciated.

The best way is to create a .htaccess file if you don't have one already and stick it in your html folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^old/page new/page [R=301,L] #can copy and paste this as many times as needed
</IFModule>

This will redirect everything from olddomain.com to newdomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,QSA,L]
You can put that mod_rewrite code into your olddomain's .htaccess.

If your hosting place does not support mod_rewrite (yep, there are such hosting companies) or it does not work for some strange and unknown reason, go with a simpler and widely available directive: Redirect or RedirectMatch.
Put such line into your .htaccess on old domain:
Redirect 301 / http://www.newdomain.com/
The above line will redirect ALL URLss from old domain into new domain suing 301 code (Permanent Redirect) which is the right thing from SEO perspective.
If you need to redirect such a single page or point it to a specific new location, use such rule (the same as above just specifying exact URL):
Redirect 301 /help-shipping.html http://www.newdomain.com/help/shipping.php
Apache mod_rewrite manual has a page: When NOT to use mod_rewrite. This particular scenario is listed there (Redirect is much lighter that RewriteRule in terms of resources, which can be an issue on very busy servers).

There is a way to do it via php (.htaccess is the best), but this has come in handy for me once or twice.
<?php
//this can be set however you need it to be
$dynamic_redirect = "http://foo.com".$your_variable;
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dynamic_redirect);
exit();
?>

Related

PHP Permanent Redirection

I'm having a strange issue with a permanent redirection in PHP.
Here's the code I'm using:
if ($_SERVER['HTTP_HOST'] != 'www.mydomain.ca')
{
header("HTTP/1.1 301 Moved Permanently");
$loc = "http://www.mydomain.ca".$_SERVER['SCRIPT_NAME'];
header("Location: ".$loc);
exit;
}
So, the home page, referenced either by www.myolddomain.ca or www.myolddomain.ca/index.php both work but every other page on the site fails to redirect. I've spent a couple of hours looking at this from all the angles I know and can't fathom it. Does anyone have any idea what the issue could be?
As a note, I've tried this without the 301 header too and get the same issue.
If you have a ftp-account to the server, you can create a .htaccess file and let it handle the requests. Just create a file named .htaccess in the root-folder of your site and post the code, changed to your desired pattern(s)
Options +FollowSymLinks
RewriteEngine on
RewriteCond {HTTP_HOST} ^yourdomain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
If you want to do it fix via PHP, I would create a redirect.php and include it on every site you need it. Hard to tell if this is the best solution, it is a bit depending on your way of layouting and structuring.
Try it by htaccess, create .htaccess file and try something like this
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
** Apache Mod-Rewrite moduled should be enabled

Proper htaccess redirect

Two questions,
I just finished a site and it is about to go live.
It is saved in www.example.com/store/, and I want it to strictly open at the /store/, nowhere else since there are many other areas on that drive, so first off, for achieving this action;
would I simply put inside the htaccess;
Redirect 301 / http://www.example.com/store/
??
And continuing off that, say I have links on /store/index.php that link off to www.example.com/links/ which is outside the /store/ link.. would those now not work?
Thanks for the help guys!
If you want all traffic be redirected to store/, just do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/store/$
RewriteRule (.*) /store/ [R=301,L]
Now all traffic will redirect to the store/.
Let me know if I could provide you with more details.
You could also stay with mod_alias RedirectMatch. The Redirect directive also include sub-directories, so you're actually creating a loop when you redirect / into a sub-directory. But RedirectMatch won't:
RedirectMatch 301 ^/(?!store)(.*)$ /store/
Also make sure to restrict access outside of that directory. So make only that directory available, and ban access to all others. Check this out for some help.

Using 404 page as redirect system?

I have to move a entire site to a new domain, the old domain will be used for another site, so I wish delete all files in old domain.
Can I delete all file and use 404 error page to redirect traffic to new pages?
example:
if($page == "http://www.site1.com/pagexxx.htm") {
header( 'Location: http://www.site2.com/new_page.html' ) ;
}
It technically and SEO correct?
Another method is to create another php file on your old domain, and in .htaccess add the following:
ErrorDocument 404 /dir/.../file.php
You just need to get the request uri in php and process the url and use the header function to redirect.
If you are doing 1:1 redirection, then just setup a redirect rule on your web server.
If, instead, you want to inform users that the site as moved, and redirect everything to the front page of the new site, I'd do the following:
1.) Create a redirect/rewrite rule on the web server to direct everything to your "This site has moved" page. I'd prefer a rewrite, so that they can easily change existing bookmarks.
2.) Put a message informing them of the move, a link to the new site, and a meta-refresh to automatically redirect after 5-10 seconds.
I would try it in .htaccess create or update an .htaccess file and put it in the root of the old domain with this below. If you are just changing domain names and files locations are still the same, this will redirect all traffic from old to new.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

301 redirect in .htaccess for 30,000 errors

I've been tasked to clean up 30,000 or so url errors left behind from an old website as the result of a redesign and development.
I normally use .htaccess to do this, but I doubt it would be wise to have 30,000 301 redirects inside the .htaccess file!
What methods have some of you used to solve this problem?
Thanks in advance.
Here as you can do with apache httpd
RewriteMap escape int:escape
RewriteMap lowercase int:tolower
RewriteMap my_redir_map txt:map_rewrite.txt
RewriteCond ${my_redir_map:${lowercase:${escape:%{HTTP_HOST}%{REQUEST_URI}}}} ^(.+)$
RewriteRule .* http://%1 [R=301,L]
I use this rewrite rules usually directly inside apache httpd configuration.
Inside the map_rewrite.txt file you have a tab delimited file with the list of redirect in the following format:
www.example.it/tag/nozze www.example.it/categoria/matrimonio
www.example.it/tag/pippo www.example.it/pluto
www.example.it/tag/ancora www.google.com
Would be much easier if you can generalize the approach because the redirect have a common pattern. But if not, in this case you only need to add the redirected url into the list.
Take care to study the RewriteMap configuration, because you can also write the list into a different format, for example like a database table.
Please pay attention to this: I have added escape and lowercase only because there are accents into the urls I need to write. If your urls doesn't have accents, you can remove both.
If you want implement these redirects in php, here the code you need:
<?php
$dest_url = "http://example.com/path...";
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dest_url);
Create a PHP page to operate as a 404 handler. It should inspect the incoming URL, check if it should map from an old page to a new page, then issue a 301. If there is no mapping then present a 404.
Simply set this page as the 404 handler in your .htaccess and there you go. IIRC this is how Wordpress used to handle 'clean' URLs on IIS before IIS7 brought in URL rewriting without needing a 3rd-party dll.
I have made a redirect class that is on the 404 page that will check the database if there is a valid page to 301 redirect to and redirect it instead of giving the 404 page. If it can't figure that out, it marks it in the database as a 404 page, so it can be fixed later.
Thanks for help guys. I've carried out the suggested course of action from freedev but have created a separate config file within Apache.
Within the httpd.conf file I have added:
# Map settings
Include "conf/extra/map.conf"
The map.conf file:
RewriteEngine On
RewriteEngine on
RewriteMap url_rewrite_map txt:conf/map.map
RewriteCond ${url_rewrite_map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*) http://website.com/${url_rewrite_map:$1} [R=301]
The map.map file is formatted as:
/oldname/ /newname
I've added quite a few of the urls for the redirection and so far so good, it isn't having a massive impact on the server like it did when added to .htaccess

.HTACCESS - redirect www.example.com/side.php?id=* to www.example.com

I have recently assisted in moving a website from a pure development domain to a live site where there used to be a site handled by another CMS system than what we are currently using. Current system is Joomla, but I don't think it matters much for my question.
So with the current site the URLs are rewritten from the standard Joomla format to be stripped of index.php and .html suffix is added in the address, meaning that URLs look like this:
http://example.com/folder/page.html
In the old site handled by another CMS systems the URLs had the following structure:
http://example.com/side.php?id=1
We are a social organisation with many sites linking to us - also quite a few that we are not even aware of - so the problem I need to handle is this: I need to redirect all these dead links on other sites so that they simply get pointed to the root of our site.
Can anyone please explain to me how to make .htaccess redirect as follows:
/side.php?id=* to root of example.com
In this case I mean the * to mean any number as there are naturally alot of pages with different IDs.
It is not of any significance to me if they point at a www. prefix or not.
Thanks in advance for your help, I hope I have not asked a question that's been answered before but my experience with .htaccess is very limited and having searched and tried different solutions didn't do it for me.
In .htaccess in the root folder, add the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side\.php$ http://%{SERVER_NAME}/ [R=301,L,QSD] #Remove the ",QSD" for Apache <2.4.0, or to keep the query string.
The R=301 will tell browsers/search engines that the page has permanently been moved.
A rule like this should work:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} id=\d+
RewriteRule ^side.php http://example.com [L,R=301]
This will redirect externally, with a HTTP 301 response (Moved Permanently)
What you need here is some 301 Moved Permanently responses to let browsers and search engines know that you're moving pages to a new location. Instead of using a mod_rewrite to redirect all requests to this side.php page, I would analyze each page of your old website and determine where all the content has been moved. Armed with this, use the htaccess Rewrite directive to inform browsers of individual pages being moved
Redirect 301 /side.php?id=123 /about_us.html
Redirect 301 /side.php?id=456 /contact_us.html
I recommend this method because it redirects those who navigate to the outdated page to the new page that has similar content to the old one they were requesting instead of just redirecting them all to your home page.

Categories