Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm making a blog site and instead of the users blogs being called /blog?display=username I would like it to just be a sub domain such as mysite.com/username

How would I be able to make the first URL look like the second URL?

share|improve this question

migrated from stackoverflow.com Jul 24 '11 at 1:05

This question came from our site for professional and enthusiast programmers.

    
Er... That's not a subdomain. A subdomain would be username.mysite.com. –  Álvaro G. Vicario Jul 23 '11 at 16:33
add comment

3 Answers

up vote 5 down vote accepted

Place this in your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog?display=$1 [L]

This will redirect each URL which do not corresponds to an existing file (!-f) or existing directory (!-d) to the corresponding blog?... URL. [L] makes the rewriting stop here (in case you have more rules). See the mod_rewrite Guide for details.

share|improve this answer
    
Thanks that looks really helpful, Thanks would you be able to explain what the symbols mean so ill be able to edit this as I want? –  Joel Jul 23 '11 at 15:21
1  
@Joel: Have a look at httpd.apache.org/docs/2.2/rewrite (and some of the pages linked from there). –  Paŭlo Ebermann Jul 23 '11 at 16:22
    
Another resource which may be useful: Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask –  danlefree Jul 24 '11 at 9:15
    
@zgold: Thanks very much for the help. The exact URL I am trying to get is joelhoskin.net76.net/blogs/theblogsnamehere to joelhoskin.net76.net/blogs/blog?display=theblogsnamehere I've put this in the .ht access file which doesn't seem to be working RewriteEngine On RewriteCond %{HTTP_HOST} ^joelhoskin.net76.net/blogs/(.*)$ [OR] RewriteCond %{HTTP_HOST} ^www.joelhoskin.net76.net/blogs/(.*)$ RewriteRule ^$ joelhoskin.net76.net/blogs/blog?display=$1 [L] Sorry this comment seemed to remove my link break, should I start a new question? –  Joel Jul 24 '11 at 23:04
    
Realized I missed off .php from the rewrite rule, still doesnt work though –  Joel Jul 24 '11 at 23:18
add comment

You are probably looking for mod_rewrite. It's easy, just put this in your .htaccess file.

RewriteEngine On
RewriteRule ^(.*)$ /blog?display=$1
share|improve this answer
add comment

You can use the PATH_INFO variable. More here: http://httpd.apache.org/docs/2.0/mod/core.html

Previous answer (using Request.PathInfo) was tarted at asp.net -- thanks for pointing that out.

share|improve this answer
    
ASP.NET is not really applicable on Apache, is it? –  Paŭlo Ebermann Jul 23 '11 at 16:28
    
Oops -- thank you -- I've modified the answer. –  kuru kuru na... Jul 25 '11 at 14:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.