0
votes

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.

1
You want every request to be redirected to either index.php or index.html? Do you mean a 301 redirect or do you mean a translation? - Jeremy Stein
I would like every request to be redirected. If user enters domain.com he would be redirected to domain.com. If users enters domain.com he would be redirected to domain.com/home/index.html. And I mean 301 redirect. - Bostjan
So I put this code to my .htaccess file? - Bostjan
No, I'm trying to understand the behavior you want. The left shows where the browser attempts to go, and the right is where the user would be redirected. That can't be right, but it's what you've indicated. Please clarify what you want. - Jeremy Stein
OK, here are the directions I want: www.domain.com -> domain.com/home/index.html domain.com -> domain.com (index.php - only index in root!) - Bostjan

1 Answers

1
votes

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]