Redirecting

When a user enter on (www.example.com), i need to redirect him to (www.example.com/site). But i don’t want to change the URL on the browser, I need it to be transparent.
I used mod_rewrite on htaccess file, like this:

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/?$ “http\:\/\/www\.example\.com\/site” [R=301,L]

But it’s not acting like I want to.

Can anybody help?

Thanks :slight_smile:

Hi, and welcome on SitePoint :wave:


RewriteCond %{HTTP_HOST} ^example[color=red]\\[/color].com$ [OR]
RewriteCond %{HTTP_HOST} ^www[color=red]\\[/color].example[color=red]\\[/color].com$

You need to escape the dots (see backslashes in red) in RewriteConds because they are regular expressions and a dot stands for “any character” in regex. While it will work if you don’t escape it, it’s technically not correct.

Also, why do you have both the www-version and the non-www version of your site available? It’s better to pick one (doesn’t matter which one) and stick with it. Also redirect the one you don’t want to the one you do want.


RewriteRule ^/?$ "http\\:\\/\\/www\\.example\\.com\\/site" [R=301,L]

  1. Don’t put double quotes around the URL
  2. Why are you escaping all that stuff? You don’t need to do that
  3. The R=301 is what is making Apache change the URL of the browser. Drop it. Once you drop it, also remove the http:// part, because that will also force an external redirect.
  4. If you’re on apache 1.x change ^/?$ to ^/$ . If you’re on Apache 2.x change it to ^$

So:


RewriteRule ^/?$ site [L]

:slight_smile:

First of all, thanks a lot for the help!

I changed to:


RewriteCond %{HTTP_HOST} ^www\\.example\\.com$
RewriteRule ^$ site [L]

It redirects, but it’s still putting the /site after the redirection… :frowning:

try changing site to site/index.php or whatever the DirectoryIndex for that site directory is :slight_smile:

Hmm, now it works!

But it’s not loading the css and js… I have to change all the paths?

I’m using like: “resources/css/style.css”

Thanks for the attention!

Please read the section “Relative links are missing!” on http://www.datakoncepts.com/seo :slight_smile: