Question

I have created a subdomain with this .htaccess; it is working fine on my www.example.com domain but not on www.example.pk. I do not understand where the problem is.

www.example.com

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?index\.php$ 
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]

www.example.pk

RewriteCond %{HTTP_HOST} !^www\.example\.pk$ [NC]
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.pk$ [NC]
RewriteCond %{REQUEST_URI} !^/?index\.php$ 
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]

I created www.example.com domain then the subdomain with cpanel *.example.com. Is it necessary or not?

No correct solution

OTHER TIPS

When you work with subdomains, you need to configure your DNS correctly to direct subdomains to your server. The subdomain-feature of cpanel might have done this for you, or it already contained a wildcard cname dns record. DNS records are usually cached, but I am unsure if this is the case for non-existing records too.

Does the subdomain already resolve to my server?

First check if your subdomain already resolves to an ip. Open a terminal and type ping yoursubdomain.example.com (obviously you need to replace this with your actual domain). If it can't be resolved to an ip, then there is probably a problem with your DNS record. You can do that online if you want here or on several other, similar, sites.

Editing your dns

If your host gives you an opportunity to edit the DNS records manually, you'll need to know the following:

  • There is 1 A-record in your dns. This is how the name is translated to an ip.
  • Any subdomains are linked to that record using CNAME-records.

You can either add a wildcard-subdomain (which will translate any (un)thinkable subdomain to your site) or a specific subdomain to your dns. You do so by adding

CNAME  (from) *.example.com (to) example.com

The TTL (time to live) is how long a record can be cached by others. It will, at most, take TTL to get everyone in the world to 'notice' changes you make in a record of your dns. This site gives some more information about that.

.htaccess

I am not sure what kind of urls you want to rewrite, but there are a couple of things you can change:

  • %{REQUEST_URI} always starts with a /. There is no need to add a ? behind it in your condition.
  • For readability, if you use %1-type-of-backreferences, put the corresponding condition directly above it, even if it turns out they are not overwritten by other conditions without capture groups.
  • ^(.*)/*([0-9]+)*$ doesn't make much sense to me. It matches every url, because everything after (.*) is optional, and can be there an infinite amount of times. I doubt this is what you wanted it to be.

RewriteCond %{HTTP_HOST} !^www\.example\.pk$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.pk$ [NC]
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top