Using 404 page as redirect system? - php

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>

Related

WP Site not working properly without www prefix

So depending on what I set the WP Settings URLs to, the site either only works properly with the www prefix or without.
Working properly with www prefix
Without www prefix
Changing the Wordpress Address and Site Address to the address without www prefix and the problem is the other way around. (blendpunkt.at is working but not www.blendpunkt.at)
You have to update the domain to use the variant without www using plugins like Better Search Replace, Velvet Blues Update URLs and the general settings of WordPress in the backend (Home and Site URL).
https://wordpress.org/plugins/velvet-blues-update-urls/
https://wordpress.org/plugins/better-search-replace/
Then you have to update https://wwwblendpunkt.at to https://blendpunkt.at or opposite. And also check if there are http variants.
You might also want to enforce https (using HSTS or another solution, see also https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess and the sections for https + www redirection and enforcing https).
Please create a full backup before using these plugins
Because as you can see www.example.com and example.com are different hostnames and cause a CORS (Cross-Origin Resource Sharing) error.
PS: This question would fit better on https://wordpress.stackexchange.com/
This seems to be some kind of a work-around and there might be other solutions. But in the way you ask, it does not matter for you if you use the url with or withour prefix, you just want the map to work. So it is an option to redirect all requests without www to the request with the www prefix.
Change your wordpress and site url to your domain with the www prefix.
In the root folder of wordpress when accessing it with FTP, there should be a .htaccess file. If there is no such file, you can create one (https://wordpress.org/support/article/htaccess/).
Inside of this file, right under the RewriteEngine on you insert this code:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
This way all requests without https and/or without www will be redirect to https://www.
Another way, without having effect on the whole website, you can just create a redirect for this specific page. Adjust this code snippet to your domain and put it into the functions.php file of your theme, or alternatively create a plugin for redirects, if you have multiple of them.
add_action( 'template_redirect', function() {
if( ( is_page('kontakt') ) ) {
wp_redirect( 'https://www.yoursite.com/kontakt' );
exit();
}
});

htaccess redirect to subdirectory by default, but open main site from link in subdirectory site

This is my example site
http://www.example.com/ - (main site, in English)
http://example.com/mylang/ - (site in my language)
Both the sites run with multiple installation of wordpress.
Now, when user enter my website, he must be redirected to http://example.com/mylang/ and there is a link to go to main site http://www.example.com/ if the user wants to see.
How can I do with .htaccess? I am open to get other (php or javascript redirect) solutions also.
On main site check if a session is set, if not redirect. (Note this must be done before any output is sent)
session_start();
if(!isset($_SESSION['been_here'])) {
header('Location: http://example.com/mylang/');
exit();
}
On the mylang set the session.
session_start();
$_SESSION['been_here'] = 1;
The duration of this session won't redirect on front page visits.
You should put below code inside .htaccess of root folder of main site
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://example.com/mylang [R=301,L]

.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.

htaccess 301 redirect url

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();
?>

URL Problem in Ajax in PHP

When I open my site without "www", like http://mysite.com/, then there is a problem with my website hit counter on the home page, which is done through AJAX.
The problem is that counter Image is not getting displayed.
It is showing blank.
There are similar problem on other pages where I have used AJAX to retrieve data.
To the cross-domain security policy, "mysite.com" and "www.mysite.com" are different domains, therefore AJAX requests aren't allowed between them.
The simplest solution is to take the domain out of your AJAX call and use a relative url, for example "/dir/ajax-callback.php" instead of "http://www.mysite.com/dir/ajax-callback.php"
You can create .htaccess file in your root dir and put this text inside
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
This will make sure that every time user enters http://mysite.com, it gets redirected to http://www.mysite.com
The server has to support .htaccess and mod_rewrite

Categories