0

Imagine in my website I want to show some analytic about domains, working URL example of what I need:

http://whois.domaintools.com/google.com

As you see in the above URL, it's handling google.com as a variable and pass it to another page to process the given variable, that's exactly what I want.

So for detecting that kind of variable, here is my regex:

/^[a-zA-Z\d]+(?:-?[a-zA-Z\d])+\.[a-zA-Z]+$/

The above RegEx is simple and accepts everything like: google.com, so in my .htaccess file I have:

RewriteRule (^[a-zA-Z\d]+(?:-?[a-zA-Z\d])+\.[a-zA-Z]+$) modules/pages/page.php?domain=$1

The above rule do what I want, but it also redirects my homepage to page.php while there is nothing in the URL, forexample: http://mysitename.com is now being forwarded to page.php

How can I fix this?

Thanks in advance

behz4d
  • 1,819
  • 5
  • 35
  • 59

1 Answers1

2

It redirects also the base domain to page.php because of the regex. You are using the + on all places, the meaning of the plus is "Matches the preceding pattern element one or more times.". (http://en.wikipedia.org/wiki/Regular_expression) If you request the homepage, it redirects because all the elements are appearing zero times, like you defined in the regex.

Instead of the + you should define a minimum and a maximum amount of characters (so the zero occurrences are not evaluated). BTW, a quick search in google for "regex domain" will output a lot of results, which are tested. Use the following for example:

RewriteEngine on
RewriteRule (^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})$) modules/pages/page.php?domain=$1

Reference: Domain name validation with RegEx

Update 1:

If you want to use your own regex, exchange the last "+" with {2,}. The top-level domains have usually at least 2 characters.

RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.html|\.php|\.pdf|\.gif|\.png|\.jpg|\|\.jpeg)$
RewriteRule (^[a-zA-Z\d]+(?:-?[a-zA-Z\d])+\.[a-zA-Z]{2,}$) modules/pages/page.php?domain=$1
Community
  • 1
  • 1
meberhard
  • 1,797
  • 1
  • 19
  • 24
  • ty for the answer, but I was using that regex because I don't want to validate sub-domains, protocols, etc, how `my own regex` should be edited? – behz4d May 01 '14 at 10:17
  • I edited the question and added a change for your regex! – meberhard May 01 '14 at 10:32
  • 1
    Probably it happens for the following reason: Apache is configured to load either index.html or index.php when the homepage is requested. Index.html and index.php will both be evaluated by the regex. So you need to exclude such filetypes with RewriteCond before the RewriteRule `RewriteCond %{REQUEST_URI} !(\.html|\.php|\.pdf|\.gif|\.png|\.jpg|\|\.jpeg)$` (I edited the answer but can't see the update yet). – meberhard May 01 '14 at 10:53