Redirect www.domain.com/example.com to example.com

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

+4
source share
2 answers

If you create an htaccess file at the root of your document and add this:

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

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

eg. 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 help in creating the domain to redirect to. If we build it in a function like get_url() , you can change this 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); ?> 
0
source

All Articles