Hi,

I am trying to write a .htaccess script that can redirect example.com to example.com/subdomain. This is my code but it's not working. Can you please suggest.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/subdomain/ [R=301,L]

Thanks in advance

Recommended Answers

All 5 Replies

Not sure you need to treat the url as a regex in your condition. This should work:

RewriteCond %{HTTP_HOST} www.example.com [NC]

Also, requests to the subdirectory itself will satisfy your condition, so you have a circular reference there. You need a new condition that causes the rule to only work on requests for resources NOT in your /subdomain/ directory.

thanks for giving replay

RewriteCond %{HTTP_HOST} www.example.com

If i'm using the above condition then it's redircting but the page is not opening.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/subdomain/ [R=301,L]

If i'm using the above rule then it's redirecting to "http://example.com/subdomain/" and

page is also opening, but i want "www" before "example.com".

So put www in your rule.

if i'm adding www then the page is not opening

Make sure your DNS is set up so www.example.com is there. If it's not, then requests to www.example.com won't work as that doesn't exist. You just need to make it the same as your example.com entry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.