Link to home
Start Free TrialLog in
Avatar of fermium
fermium

asked on

.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.
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

hi,

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

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

chk this one

Avatar of fermium
fermium

ASKER

@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.
Avatar of Shinesh Premrajan
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

Avatar of fermium

ASKER

@shinuq:

Sorry that does not work at all.

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

May be this should work
Avatar of fermium

ASKER

@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.
Much shorter and an alternative is

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

 
Avatar of fermium

ASKER

@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
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]


Avatar of fermium

ASKER

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?
Avatar of fermium

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of HackneyCab
HackneyCab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fermium

ASKER

Wow you are a Genius.