Britteny Shaefer
Britteny Shaefer

Reputation: 51

redirect www.domain.com/example.com to example.com

Many years ago i read there was a simple php script that would redirect your website like this http://example.com/google.com to google.com and it would work for any domain on the right of the forward slash. I forgot what this script is or where to find it

Upvotes: 4

Views: 115

Answers (2)

will-hart
will-hart

Reputation: 3831

If you want your redirect to be permanent, use a 301 redirect. This is search engine safe and smart browsers can upgrade bookmarks using this.

e.g. http://www.phpjunkyard.com/tutorials/php-redirect.php

<?php 
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://www.New-Website.com"); 
?>

EDIT (Response to comments below)

This post (http://stackoverflow.com/questions/8775374/how-to-get-everything-after-the-domain-name-into-a-string) will be helpful in building up the domain to redirect to. If we build that into a function such as get_url(), you could then change the above to

<?php 
    my_url = get_url();

    // CHECK IT IS A SAFE URL

    // REDIRECT
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: " . my_url); 
?>

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143876

If you create an htaccess file in your document root, and add this:

RewriteEngine On
RewriteRule ^/?([a-z0-9-.]+)$ http://$1/ [L,R]

Upvotes: 5

Related Questions