R – URL redirection added to .htaccess

.htaccessapachemod-rewrite

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.

Best Answer

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]
Related Topic