views:

93

answers:

1

Can anyone help me with adding the redirect rules to my .htaccess? I want to redirect websites visitors based on the URL they enter. For example:

  1. If a visitor enters URL without www to be redirected to my root (index.php)
  2. If a visitor enters URL, but with the WWW to be redirected to http://domain.com/home/index.html


Edit: From your description above and your comment below, it sounds like you want requests to be redirected like this:

www.domain.com             -> domain.com/home/index.html
www.domain.com/about.php   -> domain.com/home/index.html
domain.com                 -> domain.com/index.php
domain.com/home/index.html -> domain.com/index.php
domain.com/news.php?id=5   -> domain.com/index.php

If not, please replace this edit with some corrected examples.

A: 

Try these rules:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://example.com/home/index.html [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ http://example.com/index.php [R=301,L]

But note that if you’re redirecting to the same host (like the first rule probably does), you will get an infinite recursion. So you might want to delimit the first rule by excluding /home/index.html:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^home/index\.html$ http://example.com/home/index.html [R=301,L]
Gumbo
Gumbo thank you for your answer but if I add this lines to my .htaccess file, all my visitors are redirected to http://example.com/home/index.html directory.I would like to redirect those who enters url without www part to root directory like example.comhttp://www.example.com --> http://example.com/home/index.htmlandhttp://example.com --> http://example.com (where index.php is)
Bostjan
Ok, problem solved. All I needed wasRewriteEngine onRewriteCond %{HTTP_HOST} ^www\.RewriteRule ^ http://example.com/home [R=301,L]Thank you!!!!
Bostjan