8

When people access my domain it is redirected to http://www.mydomain.com/en/index.php using php code.I added the following code in .htaccess

RewriteEngine on
Options +FollowSymlinks

RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RedirectPermanent /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php

to redirect people from non www to www,

Still users can access by typing both http://mydomain.com/en/page-a1/abc.php and http://www.mydomain.com/en/page-a1/abc.php URLs

Does anyone know the method to completely redirect to http://www.mydomain.com/en/page-a1/abc.php even if user typed http://www.mydomain.com/en/page-a1/abc.php, and prohibit accessing URLs without www.

Ullas Prabhakar
  • 416
  • 2
  • 10
  • 24
  • 1
    The idea of that rewrite is to redirect user to www if he comes to non-www. So non-www is still accessible, but tells browser to redirect to www and keep that url in the cache, as this is permanent (301) redirect. So, from user's point of view, when he is accessing `domain.com`, browser automatically redirects to `www.domain.com`. Do your users see `domain.com` in browser's address bar? – J0HN Aug 29 '11 at 10:04
  • No, user wont see domain.com, but he can open http://mydomain.com/en/page-a1/abc.php page in the browser. – Ullas Prabhakar Aug 29 '11 at 10:24
  • And what do you want to do? Prohibit the user typing that URL in browser?:) If you want to deny `domain.com` service at all, just redirect it to `404` page, with something like `RewriteRule ^(.*)$ 404.php [L]` – J0HN Aug 29 '11 at 10:28
  • Possible duplicates: http://stackoverflow.com/questions/4042309/www-and-non-www-sites, http://stackoverflow.com/questions/1685962/htaccess-redirect-non-www-to-www-preserving-uri-string, http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www, http://stackoverflow.com/questions/3707319/non-www-to-www-htaccess-redirect, etc – goodeye Aug 29 '11 at 14:06

6 Answers6

13
$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
    exit;
}

in php

genesis
  • 50,477
  • 20
  • 96
  • 125
5
<?php
$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit;
}
?>

Working Properly

Ashish
  • 51
  • 1
  • 1
4

I'm not sure how to do it through .htaccess, but I do it with PHP code myself within my config.php which is loaded for every file.

if(substr($_SERVER['SERVER_NAME'],0,4) != "www." && $_SERVER['SERVER_NAME'] != 'localhost')
    header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);

EDIT: @genesis, you are correct I forgot about https

Change

header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);

to

header('Location: '.
       (@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').
       'www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
Kokos
  • 9,051
  • 5
  • 27
  • 44
  • You should use HTTP_HOST instead of SERVER_NAME; (and strncmp() instead of substr() ;) ) – Arnaud Le Blanc Aug 29 '11 at 10:12
  • I used HTTP_HOST before, I switched to SERVER_NAME because it includes the final `/`. Why should I use HTTP_HOST instead? – Kokos Aug 29 '11 at 10:14
  • 1
    HTTP_HOST is the Host header sent by the client; I believe SERVER_NAME is based the `ServerName` directive of the VirtualHost (and does is not necessarily the real hostname of the url) – Arnaud Le Blanc Aug 29 '11 at 10:22
2

Add RewriteEngine On before RewriteCond to enable your rewrite rules:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$  http://www.%{HTTP_HOST}/$1 [R=301,L]

And if you have https:

RewriteEngine On

RewriteRule .? - [E=PROTO:http]

RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$  %{ENV:PROTO}://www.%{HTTP_HOST}/$1 [R=301,L]
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0
Redirect 301 /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on

# mydomain.com -> www.mydomain.com
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
</IfModule>
Shef
  • 44,808
  • 15
  • 79
  • 90
  • If you have other rules in your `.htaccess` file you should update your question to add them so we can advice where to place the code. – Shef Aug 29 '11 at 10:11
  • Hi shef,along with the above mentioned code there is few RedirectPermanent,redirect 301 code also there. – Ullas Prabhakar Aug 29 '11 at 10:20
  • @Ullas Prabhakar: If you are using `redirect 301 ...` sytanx, than it should be outside the `IfModule`, otherwise you should edit your question to add all the code so I can suggest where to place the above code for it to work. – Shef Aug 29 '11 at 10:28
  • @Ullas Prabhakar: I have updated the code, make sure to change `mydomain` with your actual domain name. Just drop it all in the `.htaccess` file and it should work flawlessly. – Shef Aug 29 '11 at 11:01
  • Thanks for your help, but even the code you suggested is also not working, though the redirect 301 is working – Ullas Prabhakar Aug 29 '11 at 11:45
  • @Ullas Prabhakar: How are you testing it? Are you trying to visit `http://mydomain.com/en/page-a1/abc.php` and it is not redirecting you to `http://www.mydomain.com/en/page-a1/abc.php`? – Shef Aug 29 '11 at 12:41
  • Yes, i tried opening both URL's mydomain.com/en/page-a1/abc.php and www.mydomain.com/en/page-a1/abc.php,Unfortunately both are working.The URL without www is not redirecting to URL with www. – Ullas Prabhakar Aug 30 '11 at 05:10
  • @Ullas Prabhakar: That's the code to redirect non-www to www, there is no doubt about that, because I am using it in production servers. The last question to be asked: do you have the module `mod_rewrite` enabled? – Shef Aug 30 '11 at 06:40
  • Yes mod_rewrite is enabled, since `Redirect 301 /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php` is working, but this Non-WWW to WWW is the only thing not working.Do you know any other method to do this?? – Ullas Prabhakar Aug 30 '11 at 07:11
  • @Ullas Prabhakar: `Redirect 301 ...` does not require `mod_rewrite` it requires `mod_alias`. Please check if you have `mod_rewrite` enabled. – Shef Aug 30 '11 at 07:19
  • :Yes,mod_rewrite is enabled. I checked using the methods mentioned in this two links [www.workingwith.me.uk](http://www.workingwith.me.uk/articles/scripting/mod_rewrite) and [www.webune.com](http://www.webune.com/forums/how-to-test-check-if-mod-rewrite-is-enabled-t40.html), which are working – Ullas Prabhakar Aug 30 '11 at 07:49
  • @Ullas Prabhakar: I don't know what you are doing wrong, but I am 100% sure that the above code works, because, as I said, I am using it in my production servers. – Shef Aug 30 '11 at 07:54
  • I had tried your code and the code i had mentioned,dont know why RewriteRule is not working but rest of the codes are working like redirect 301, Redirectpermanent.I have checked in phpinfo, both mod_alias & mod_rewrite are enabled.Can you suggest any other way or anything else do i need to check to rectify this issue. – Ullas Prabhakar Aug 30 '11 at 08:55
  • @Ullas Prabhakar: Can we access your site somewhere? Or are you working in a local machine? – Shef Aug 30 '11 at 08:57
  • @Ullas Prabhakar: Where have you placed the `.htaccess` file? It must be above `/en/`, that is, it must be on root in order for it to work. – Shef Aug 30 '11 at 13:11
  • :Yes,Shef I placed it in the root. – Ullas Prabhakar Aug 30 '11 at 13:13
0

I think you want to redirect the user instead of rewriting the URL, in that case use Redirect or 'RedirectMatch` directive. http://httpd.apache.org/docs/2.3/rewrite/remapping.html#old-to-new-extern

Ravi
  • 727
  • 6
  • 19