So I've moved my website from site.com to sub.site.com. I have also moved the booking system.
Is it possible for me to have PHP code on index.php on site.com to check if the user wanted site.com/unbook.php to automatically redirect them to sub.site.com/unbook.php
The optimal thing would be some kind of "redirect everything if there is anything in the url other than site.com"
Something like
$urlafter = explode("site.com", $url);
if (strlen ($urlafter[1]) > 0 ) {
header("Location: sub.site.com".$urlafter[1]);
exit();
}
Or would it be better using something like $_SERVER[REQUEST_URI] or even .htaccess
-- EDIT --
Non duplicate because they only show .htaccess answers, I'd prefer PHP
Using Htacces is better for the scenerio.
In the .htaccess file at the site.com you can set the following rule to redirect:
Redirect 301 / http://sub.site.com/
301 is for permanent redirect.
If the redirect is temporary, you can use 302 instead.
In Php you can use the following code to replace the domain:
$uri = $_SERVER['REQUEST_URI'];
$old_domain = 'yoursite.com';
$new_domain = 'sub.yoursite.com';
$old_domain_pos = strpos($url, $old_domain);
$redirect_url = substr($url, 0, $old_domain_pos).$new_domain.substr($url, ($old_domain_pos+strlen($old_domain)));
The above code replaces the domain only once, leaving everything else as it is.
If your url is like the following, where the domain may repeat in the querystring, which themselves need to be replaced.
http://www.yoursite.com/get/?assset=www.yoursite.com/imgs/pic01.png
then you can consider string_replace method.
$redirect_url = str_replace($old_domain, $new_domain, $url);
Related
I moved approx 100 articles from my old website to a new one. I want to create a redirect in the old site's header, so if the old URL of an article would be visited, the visitor should be redirected to the new URL of that article.
However, just replacing the domain won't do the trick, as I changed the permalink of the articles. So I would need some "database" (with arrays?) which would decide if the actual URL has a redirect in the database, like:
// "the old permalink" = "the new permalink"
$urlpermalink["article-cars"] = "http://NewWebsite.com/new-cars-article";
$urlpermalink["an-article-dogs"] = "http://NewWebsite.com/new-dogs-text";
$urlpermalink["old-text-trees"] = "http://NewWebsite.com/new-blogcontent-about-trees";
So for example if a visitor visits "http://OldWebsite.com/article-cars", he should be redirected to "http://NewWebsite.com/new-cars-article", because that's how the database/array says.
And so I could something like this:
$visitingurl = $_SERVER[REQUEST_URI]; // Getting the URL the visitor is on now
foreach( $urlpermalink as $value ) { // For every entry in the database/array...
if (strpos($visitingurl, $urlpermalink) !== false) { // check if the visitingurl contains that (like "article-cars")
// The visitor is indeed on an old URL which is in the database/array, so let's redirect him to the new URL
header("HTTP/1.1 301 Moved Permanently");
header(url . $_SERVER['QUERY_STRING']);
exit();
}
}
Of course this code is totally wrong, but I have almost no idea about PHP, so could you please help me solving this? (a JavaScript-solution is fine too, or htaccess, or whatever :) )
Thank you very much!
I think I solved this now with htaccess (I have no idea about htaccess either, just googled even more).
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectMatch 301 ^/article-cars/ http://NewWebsite.com/new-cars-article
RedirectMatch 301 ^/an-article-dogs/ http://NewWebsite.com/new-dogs-text
RedirectMatch 301 ^/old-text-trees/ http://NewWebsite.com/new-blogcontent-about-trees
</IfModule>
Is this okay this way?
I have a multilanguage website with this structure http://www.url.com/en/, http://www.url.com/it/, http://www.url.com/pt/ etc.
My problem now is that when you access the link url.com I will have 3 redirects.
To http://url.com
To http://www.url.com
To http://www.url.com/en/
How can I do to have only 2 redirects? If you access http://url.com to redirect to http://www.url.com/en/
In my htaccess file I have this lines.
RewriteCond %{HTTP_HOST} ^url.com [NC]
RewriteRule ^(.*)$ http://www.url.com/$1 [L,R=301]
This one redirects from url.com to http://www.url.com. After this is redirected a php file script recognized the domain and it redirects again to /en/
$url= strpos($_SERVER['REQUEST_URI'],"/".$lang."/");
if($url!==0){ Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.url.com/en/" ); }
After that I have some conditions to check if you are in another language.
if($lang == "pt"){$langcheck = "pt"}
My links are created like this:
RewriteRule ^([_A-Za-z0-9-]+)\/$ index.php?page=index&lang=$1 [L]
So what should I write in htaccess to work this for each language. Because if I change the RewriteRule to url.com/en/ the other languages doesn't work anymore.
How to accomplish this depends upon where you get the language string from, how many languages you want to support, and how complex this interaction should be.
If it's just a couple of languages, and you want to use the browser settings for this, you might be able to use htaccess rewrites for this.
However, if you have more than just a few languages, want to let users select their own language in the site itself, or anything more complex than the above. Well, then you need to do everything in PHP.
This means removing the htaccess redirect for the missing "www" domain, then checking for this in the PHP code. Before, or after, you check for the language selected. Using both checks to determine the link to redirect to, before actually redirecting.
Meta example:
// We want all users to use the domain with www.
$redir = '';
if (substr ($url, 0, 3)) != 'www') {
$redir .= 'www.';
}
// If user has selected a different language, we need to redirect for that too.
if (!empty ($lang)) {
$redir .= "{$base_url}/{$lang}/";
}
// If $redir isn't empty, redirect the user.
// PS: Always remember to use `die()` after a `header()` redirect!
I want to make a service that allows users to get a direct link to do an action like follow or subscribe (not telling for obvious reasons).
So using htaccess or php (whatever is better), how can I do the following
example.com/insertusernamehere automatically redirects to somepopularsocialmediasite.com/?follow_user=blahblahblah
It's important to note that the example.com site would be on a different server than somepopularsocialmediasite.com.
Also I would like to have some .html pages including an index.html, about.html, etc etc so I would need a way to exclude certain queries/files from redirecting.
I'm assuming you can store the username in a variable somehow, so we could do something like this:
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$username = 'some_username_you_get_somehow';
$url_username = substr(strrchr($current_url, '/'), 1);
if ($url_username == $username)
{
header("Location: http://somepopularsocialmediasite.com/?follow_user={$username}/");
}
If all the pages that will exist on your server (index.html, etc) all have the .html extension, you can simply do this:
RewriteRule ^([a-zA-Z0-9]+)$ http://socialsite.com/?follow_user=$1 [R=301, L]
Basically, that rule will not match anything that contains an extension, because it's only looking for non-case-sensitive letters and numbers. You can even put hyphens in there if you like.
I want to rearrange my web site, and move all customer pages into a directory (/customerPages), while keeping the same URL (URL to access the page and the URL that is showed in the browser). I'm using Apache and PHP (CakePHP).
I've tried rewiring my 404 error page in the following way:
// Redirect customers to customers landing pages under /customerPages
if (strpos($message,'customerPages') == false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
$homepage = file_get_contents($url);
echo $homepage;
}
But this solution breaks all images written using relative paths.
Later I've tried using redirection instead:
if (strpos($message,'customerPages') == false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
header("Location: ".$url);
}
But than the URL changes. I've tries fiddling with RewriteRule with no luck.
How can I achieve this using the first,second or any other method?
Another way, just basic idea:
1. Put in your /oldpath/.htaccess file (we handle file not found 404 error):
ErrorDocument 404 /oldpath/redirect.php
2. /oldpath/redirect.php file (our handler) - make redirect to new path only if file exists in newer location:
$url = parse_url($_SERVER['REQUEST_URI']);
$filename = basename($url['path']);
if(file_exists('/newpath/'.$filename)) {
header('Location: /newpath/'.$filename);
}else{
header('Location: /your_real_404_handler');
}
You need to redirect image requests from newer location (/customerPages) to the old path.
You can use mod_rewrite apache module to redirect such requests back:
RewriteEngine on
RewriteRule ^/customerPages/(.*\.jpg|.*\.gif|.*\.png) /oldpath/$1 [R]
OK, so I'm rewriting some page URLs for a custom PHP cart.
I've got the following rules:
RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]
These will allow me to use a url structure like example.com/product/23/product-slug.
That part is working alright. I'm wondering what options I have for the other direction; redirecting requests for the OLD url to the NEW url. So for example, when someone goes to /product.php?id=2 I want to redirect to /products/2/slug.
Any idea how to get this done?
I tried a simple redirect, but would not work:
Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Redirect only takes a url prefix, not a regex (e.g. /store.php or /store)
You need to try RedirectMatch:
RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Also, is it supposed to start with a /? I'm not sure (your RewriteRule entries above start with no slash, for example)
I solved this a different way: with a modification to the store.php file.
I looked at output from print_r($_SERVER) after pinging both the normal and rewritten urls. I found that $_SERVER['SCRIPT_URL'] contains "/store.php" when the normal url is hit and it contains my rewritten path when the rewritten url is hit.
This means I can do a simple test and redirect appropriately:
if ($_SERVER['SCRIPT_URL'] == "/store.php") {
// run some code that will generate the url
$rewrittenURL = generateURL();
// then add:
header("HTTP/1.0 301 Moved Permanently"); // tell spiders this is permanent
header("Location: $rewrittenURL");
}