Sign up ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

share|improve this question

migrated from Jun 20 '11 at 10:22

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

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 '11 at 11:42

1 Answer 1

1) On your website, make sure that you generate PROPER URLs, e.g. 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 to point into the same root folder as

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: => /topic.php?category=Minecraft => /article.php?id=1&category=Minecraft&Name=lorem-ipsum


These rules will work for any subdomains (including -- 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.

share|improve this answer
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 -> and when i create a new category ex: /topic.php?category=Ipsum it would go to // I head of something called a wildcard subdomain but not sure how to set it up – David Knag Jun 21 '11 at 23:44
@David I have updated my answer. Configuring Apache to accept any sub-domain name (* 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 '11 at 0:00

Your Answer


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.