0

Every user on my website get its own URL for usernames like username.example.com. I know that sometimes people are typing www in front the URL. When users do that, they will get redirected by the system to the frontpage. Is there a way to (1) detect the use of www, (2) the username and (3) forward the user to username.example.com, NOT www.username.example.com with PHP?

This is not a DNS-releated question.

RewriteEngine On
Options +Followsymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_FILENAME} !^.*/images/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/uploads/.*$
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.bloggify\.no(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^(.*)$ - [E=BLOGUSER:%1]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^$ /index.php?w=%1 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^page([0-9]+)/([^/]+)?$ /index.php?w=%1&page=$1$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profiles/([^/]+)/([^/]+)?$ /profile.php?u=$1$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profiles/?$ /profile.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^albums/([^/]+)/([^/]+)?$ /album.php?u=$1$2 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive/(.*)/(.*)/([^/]+)?$ /archive.php?w=%1&y=$1&m=$2$3 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive\.php/(.*)/(.*)/([^/]+)?$ /archive.php?w=%1&y=$1&m=$2$3 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive/(.*)/([^/]+)?$ /archive.php?w=%1&y=$1$2 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive\.php/(.*)/([^/]+)?$ /archive.php?w=%1&y=$1$2 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive/([^/]+)?$ /archive.php?w=%1$1 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive\.php/([^/]+)?$ /archive.php?w=%1$1 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^friends/([^/]+)?$ /friends.php?w=%1$1 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^friends\.php/([^/]+)?$ /friends.php?w=%1$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/friends/page([0-9]+)/([^/]+)?$ /friends.php?w=$1&page=$2$3 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^([0-9]+)/([^/]+).html$ /entry.php?w=%1&e_id=$1 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^([0-9]+)/([^/]+)?$ /entry.php?w=%1&e_id=$1$2 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^&([^/]+)?$ /index.php?w=%1&$1 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^([^/]+)/([^/]+)?$ /index.php?w=%1&category=$1$2 [L]
RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^([^/]+)/page([0-9]+)/([^/]+)?$ /index.php?w=%1&category=$1&page=$2$3 [L]
  • 1
    PHP does not run the web server. Check what product you are using to host your website. Apache is commonly used for this purpose. – George Apr 15 '13 at 18:03
  • Don't tell your users that their URL is `username.example.com`. That's just plain wrong, `username.example.com` is not a valid URL. Tell your users, their URL is `http://username.example.com`. I suspect no one will then be tempted to use `http://www.username.example.com` – Oswald Apr 15 '13 at 18:10
  • Thats trivial to "fix" with a rewrite rule http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www – AD7six Apr 15 '13 at 18:12
  • @Oswald bad advice - you're effectively saying "do this, and then just assume it'll never happen". – AD7six Apr 15 '13 at 18:16
  • @AD7six My main point is, not to use technical terms like *URL* in a wrong way. – Oswald Apr 15 '13 at 18:29
  • the site domain is in the rewrite rules - it doesn't do what you're suggesting. – AD7six Apr 15 '13 at 18:33

2 Answers2

1

Since "This is not a DNS-related question" I'm assuming you have a wildcard dns entry setup and have your web-server setup to load a single document root regardless of the sub-domain entered. After that, you can just check $_SERVER['HTTP_HOST'] to get the full domain. Explode() that by "." and check if [0] is www and send a 301 redirect (moved permanently) to the url without www

You can also do this with mod_rewrite in apache. You can write a RewriteCond that checks if %{HTTP_HOST} starts with www and then capture the rest of the url in the pattern and write a RewriteRule that does a 301 redirect.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
0

As @George pointed out in his comments, you would probably be best server to do this sort of URL rewrite at the web-server level, though it can be done in PHP by investigating the value of $_SERVER['SERVER_NAME'] or $_SERVER['HTTP_HOST'] (which to use would depend on the server config) and issue a redirect via sending a header in PHP (i.e. header('Location: http://username.domain.com');)

Mike Brant
  • 70,514
  • 10
  • 99
  • 103