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?

  • Er... That's not a subdomain. A subdomain would be username.mysite.com. Jul 23 '11 at 16:33

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.

  • 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). 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

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

RewriteEngine On
RewriteRule ^(.*)$ /blog?display=$1

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.

  • ASP.NET is not really applicable on Apache, is it? Jul 23 '11 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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