say i have a website with /article.php?id=1&category=Minecraft&Name=Lorem+Ipsum

and /topic.php?category=Minecraft

how would i setup url rewrite to make the topic pages being


and articles being


I couldn't find this how to do this anywhere on the web... well, at least on google

  • Cannot change the case of categories (without inserting each one manually) and cannot deal with spaces easily. Look at it in reverse, given a URL how does the web server know what the underlying page should be? Changing domain/subdomain without redirects can also unravel site security.
    – Metalshark
    Jun 20, 2011 at 11:42

1 Answer 1


1) On your website, make sure that you generate PROPER URLs, e.g. http://minecraft.domain.com/1/lorem-ipsum instead of /article.php?id=1&category=Minecraft&Name=Lorem+Ipsum

Here, the acceptable characters for the article name is:

  • any latin characters (can be mixed case, but I would recommend to have all lower case)
  • digits
  • underscore char _
  • minus char -

Any other characters should be replaced by -. If during URL normalisation you end up with more that 1 - character next to each other -- remove such extra chars. The article name should not start or end with - or _.

Example of normalisation process (step-by-step):

  • Oops! I did it again! -- initial text
  • oops--i-did-it-again- -- after replacing unwanted characters by -
  • oops-i-did-it-again- -- removing duplicated - characters
  • oops-i-did-it-again -- final sting after removing trailing -

2) Setup your subdomain minecraft.domain.com to point into the same root folder as domain.com

3) Add these rules into your .htaccess

# Activate Rewrite Engine
RewriteEngine On
# Do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# rewrite root (topic) hit
RewriteCond %{HTTP_HOST} ^minecraft\.domain\.com$
RewriteRule ^$ /topic.php?category=Minecraft [QSA,L]
# rewrite article hits
RewriteCond %{HTTP_HOST} ^minecraft\.domain\.com$
RewriteRule ^(\d+)/([a-z0-9\-_]+)$ /article.php?id=$1&category=Minecraft&Name=$2 [NC,QSA,L]

These rules will do these rewrites:

minecraft.domain.com => /topic.php?category=Minecraft

minecraft.domain.com/1/lorem-ipsum => /article.php?id=1&category=Minecraft&Name=lorem-ipsum


These rules will work for any subdomains (including www.domain.com) -- replace last 6 lines from previous snippet:

# rewrite root (topic) hit
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.domain\.com$ [NC]
RewriteRule ^$ /topic.php?category=%1 [QSA,L]
# rewrite article hits
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.domain\.com$ [NC]
RewriteRule ^(\d+)/([a-z0-9\-_]+)$ /article.php?id=$1&category=%1&Name=$2 [NC,QSA,L]

It is YOUR responsibility to generate PROPER URLs. The above rules will make these NICE urls working.

  • no, i mean like the subdomain is used based on the category so i could create an article with a new cateogyr and it would go to a new subdomain. Ex: /topic.php?category=Minecraft -> minecraft.domain.com and when i create a new category ex: /topic.php?category=Ipsum it would go to Ipsum.domain.com // I head of something called a wildcard subdomain but not sure how to set it up
    – Alice
    Jun 21, 2011 at 23:44
  • @David I have updated my answer. Configuring Apache to accept any sub-domain name (*.domain.com) is out of the scope of this answer and the whole site as well. If you need this -- then better ask it at sister site -- ServerFault. I apologies if I STILL did not understood you correctly. If that is the case -- please elaborate.
    – LazyOne
    Jun 22, 2011 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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