Sinac
Sinac

Reputation: 11694

Redirect special PHP files to Subdomains

I have many articles in foo.com/articles/

like this:

foo.com/articles/banana.php
foo.com/articles/strawberry.php

and want redirect SEO to subdirectory like this:

banana.foo.com
strawberry.foo.com

EDIT

when I type (banana.foo.com) show me (foo.com/articles/banana.php).

just show and not realy redirect

I can not create many subdomain by Cpanel, therefore (banana.foo.com) do not create by Cpanel

Upvotes: 2

Views: 497

Answers (3)

Joshua Kaiser
Joshua Kaiser

Reputation: 1479

In .htaccess you can do this with the following:

RewriteRule ^foo.com/articles/([a-z0-9-]+).php $1.foo.com [PT,L]

You'll need mod rewrite installed, and you'll need a wild card subdomain set up in your DNS server. http://en.wikipedia.org/wiki/Wildcard_DNS_record

Upvotes: 1

jaco
jaco

Reputation: 3516

You might do something like:

if (preg_match('#.*/articles/([a-z0-9\-_]+)\.php#i', $_SERVER['PHP_SELF'], $match)) {
    header('Location: http://' . $match[1] . '.foo.com');
    exit;
}

Remember to paste this code before any other output or you'll get an error.

Or via .htaccess with mod_rewrite enabled and RewriteEngine ON:

RewriteRule ^.*/articles/([a-z0-9\-_]+)\.php$ http://$1.foo.com [R,L]

EDIT

The reverse would be something like:

if (preg_match('#^http://([a-z0-9\-_]+)\.foo\.com.*$#i', $_SERVER['HTTP_HOST'], $match)) {
    header('Location: http://foo.com/articles/' . $match[1] . '.php');
    exit;
}

And with .htaccess:

RewriteCond %{HTTP_HOST} ^([a-z0-9\-_]+).foo.com$
RewriteRule .* http://foo.com/articles/%1.php [R,L]

Upvotes: 1

Mohammad Ahmad
Mohammad Ahmad

Reputation: 745

put this in your .htaccess file ( don't forget to replace "yourdomain" with your domain name)

Options +FollowSymLinks
Options +SymLinksIfOwnerMatch

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*).yourdomain.com$
RewriteRule (.*) yourdomain.com/articles/%1.php [L]

Now when you request

banana.foo.com

The result will come from

foo.com/articles/banana.php

and so on

Upvotes: 3

Related Questions