We help IT Professionals succeed at work.

.htaccess url rewrite

I need a .htaccess file which would do the following:

Rewrite

http://www.domain.com/somedomain.com
to
http://www.domain.com/check.php?domain=somedomain.com

It should only rewrite if a valid domain name is provided.
It should not cause problems with other dirs like http://www.domain.com/images/ e.t.c.

Plz help.
Comment
Watch Question

hi,

#RewriteRule ^/(.*)/?$ check.php?domain=$1

RewriteRule (.*)/?$ check.php?domain=$1

chk this one

Author

Commented:
@insoftservice:

No that does not work.

Best thing that works till now is - RewriteRule ^([a-zA-Z0-9_-]+)$ check.php?domain=$1

However it is even rewriting for non-domain: http://www.domain.com/images

So a little tweak is required to only rewrite for valid domain name. domain with extension.
Shinesh PremrajanEngineering Manager

Commented:
hope this helps
RewriteEngine  on
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteCond    %{REQUEST_FILENAME}  -f
RewriteRule   ^([A-Za-z0-9-]+)/?$   check.php?domain=$1   [NC,L]

Open in new window

Author

Commented:
@shinuq:

Sorry that does not work at all.

http://www.domain.com/somedomain.com is showing: 404 Not Found
Shinesh PremrajanEngineering Manager

Commented:
RewriteRule   ^([A-Za-z0-9-]+)/?$   http://www.domain.com/check.php?domain=$1   [NC,L]

May be this should work

Author

Commented:
@shinuq:

Sorry that also does not work.

http://www.domain.com/somedomain.com is showing: 404 Not Found

http://www.domain.com/ is working alright.
Shinesh PremrajanEngineering Manager

Commented:
Much shorter and an alternative is

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /check.php?domain=$1 [L]

 

Author

Commented:
@shinuq:

Sorry that also does not work too.

http://www.domain.com/somedomain.com is rewriting to - http://www.domain.com/check.php?domain=check.php
Shinesh PremrajanEngineering Manager

Commented:
OK, what are the possible urls that you accessing. I am trying with another example,

Does your url have only the .com as the url end.



RewriteRule ^([^/.]+)/?$ /check.php?domain=$1 [L]


Author

Commented:
Ok this is the thing that works now:

RewriteEngine  on
RewriteRule ^$ index.php [L]
RewriteRule ^index.php$ index.php [L]
RewriteRule ^domains.php$ domains.php [L]
RewriteRule ^check.php$ check.php [L]
RewriteRule ^style.css$ style.css [L]
RewriteRule ^([a-zA-Z0-9-.]+)$ check.php?domain=$1

However code looks clumsy. Is it possible to optimize it?

Author

Commented:
This works better:

RewriteEngine  on
RewriteRule ^([a-z]+)\.php$ $1.php [L]
RewriteRule ^([a-z]+)\.css$ $1.css [L]
RewriteRule ^([a-zA-Z0-9-.]+)$ check.php?domain=$1

Any more optimization?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?:[a-zA-Z0-9]+\.)+[a-zA-Z0-9]+)$ /check.php?domain=$1

This will first check that the request is not for a valid file or directory on your server, and if not it sends the requested part (so long as it fits the aA0.aA0 pattern with any number of dots) to check.php.

However, note that there's no way of Apache knowing the difference between a directory or filename and a domain name. For instance, in the future it's very possible that there could be a domain called index.html (if someone creates a .html top-level domain). So only the RewriteCond check to rule out files and directories on your server can help here.

Author

Commented:
Wow you are a Genius.