Mobile app version of vmapp.org
Login or Join
Frith620

: URL Rewrite to subdomains? 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

@Frith620

Posted in: #Apache #Htaccess #ModRewrite #UrlRewriting

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

minecraft.domain.com


and articles being

minecraft.domain.com/1/Lorem+Ipsum


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

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Frith620

1 Comments

Sorted by latest first Latest Oldest Best

 

@Gloria169

1) On your website, make sure that you generate PROPER URLs, e.g. 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=&category=Minecraft&Name= [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



UPDATE:

These rules will work for any subdomains (including 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=&category=%1&Name= [NC,QSA,L]


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme