Search code examples
.htaccessurl-rewritingsubdomainwildcard-subdomain

Redirecting dynamic subdomain with directory structure to a single file


I have a domain name with wildcard dns active. what i want to write in htaccess is that when the user type abc.domain.com/news/news-details.php then it should send request to the file news.php with arguments val=abc. I have in the htaccess the following code:

RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com
RewriteCond %{REQUEST_URI} ^news/news-details\.php$
#RewriteCond %{QUERY_STRING} !^id=.
RewriteRule (.*) /news.php?div=%1 [L]

But this is throwing a 404 error. what is the core part are the following two lines to explain

request for www.domain.com/news/news-details.php goes to www.domain.com/news.php and abc.domain.com/news/news-details.php goes like www.domain.com/news.php?val=abc Please note that i am not redirecting. the urls will show as a dynamic sub domain. physically there will be no folder. all files are located in the root.


Solution

  • RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com [NC]
    RewriteCond %{HTTP_HOST) !^www\.
    RewriteRule ^news/news-details.php$ /news.php?div=%1 [NC,QSA,L]