1

I am new to this.

Code from my .htaccess file goes like this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

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

Redirect 301 /abc/ /abcnew/

I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com

I copied:

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

this code from here Generic htaccess redirect www to non-www.

I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)

Redirect 301 /abc/ /abcnew/

and just FYI this above code works fine(its redirecting my old links to new links).

I also tried this.

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

Doesn't work for me.

Please help. Thanks in advance...

Edit:

this link I found this. But not sure what should be edited. Can anyone please point out.?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Nits
  • 629
  • 1
  • 7
  • 16

2 Answers2

2

You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.

Try this:

RewriteEngine on
RewriteBase /

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

RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks Anubhava. I tried this code but still not working. any suggestions ? – Nits Jul 16 '14 at 12:21
  • Open Firebug and then visit `http://www.example.com` in the browser. Check in Net tab how many redirects do you get. – anubhava Jul 16 '14 at 13:40
  • +1, do you think the initial slash in `/abcnew` could be causing Nits's problem? – zx81 Jul 17 '14 at 04:59
  • @anubhava: hi Anubhava., I dont see any redirects in firebug. I also tried putting this code by removing abc redirect and checked, still not working. – Nits Jul 17 '14 at 07:17
  • 1
    Ok first verify whether your `.htaccess` is enabled or not, by putting same garbage (random) text on top of your `.htaccess` and see if it generates 500 (internal server) error or not? If you get 500 error then tell me location of this .htaccess and also if there are other rules in this .htaccess as well. – anubhava Jul 17 '14 at 07:51
  • yeah checked.. this .htaccess was in wrong path.. I moved it to public folder, now got it working. Thanks a lot. But still did not get why the other redirection were working.. – Nits Jul 17 '14 at 08:51
  • Glad it worked for you. Location of .htaccess is very critical since rules change as per the directory it is residing in. – anubhava Jul 17 '14 at 10:10
0
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  RewriteCond %{HTTPS} on
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Piyush Prajapati
  • 449
  • 1
  • 4
  • 7