LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-16-2011, 01:46 AM   #1
jnyunt
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Rep: Reputation: 0
mod_rewrite with non-www to www for all deep links


I am still struggling to get non-www to www.
At the moment I have following code to redirect from non-www to www.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

It works totally fine for home page but not for other deep links, such as http://domain.com/city/Sydney.php, they are still going to non-www pages. What 's the best approach I could use to solve it?

Thanks in advance

Last edited by colucix; 01-30-2012 at 11:13 PM. Reason: Removed commercial link
 
Old 08-16-2011, 03:06 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

Your rewrite looks fine. Are you sure it's not working? Maybe you need to clear your browser cache,
You can also use the following rule:
Code:
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Regards

Last edited by colucix; 01-30-2012 at 11:14 PM.
 
Old 08-16-2011, 03:45 AM   #3
jnyunt
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 0
Hi bathory
Good to see u again. Yep I did remove ^ and $ like you suggested, no function was changed and still working as before, still redirecting non-www home page to www. but other pages still not working. I dont think it was cache, I cleared and even tested with other browser that I never used before. If you type in google as "site:domain.com" , all the non-www pages will come up and try going to one of the the page if it makes any different to you.

Thanks again

Last edited by colucix; 01-30-2012 at 11:14 PM.
 
Old 08-16-2011, 04:15 AM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Doh,

What happens if you go the other way around?
Code:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [L]
BTW "R=301" is not really needed as it's in fact an external rewrite.

Last edited by colucix; 01-30-2012 at 11:14 PM.
 
Old 08-16-2011, 07:44 AM   #5
jnyunt
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 0
it is same thing, still going to non-www
 
Old 08-16-2011, 08:34 AM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Strange, it should work either way. Try the apache examples to see if you get lucky.
You can enable rewrite logging and check where the RewriteCond fails
Code:
RewriteLogLevel 9
RewriteLog rewrite_log
 
Old 08-16-2011, 06:02 PM   #7
jnyunt
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 0
Hi Bathory,

I got it working now. I got another .htaccess in city folder, which has following,

RewriteEngine on
RewriteRule ^([^/\.]+)$ $1.php

to redirect any file with or without .php to filename.php. Somehow it was stuffing it up. so I removed city/.htaccess and work fine.
Thanks again for your help .. I will read the apache examples again to get better understand of it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache https redirect without-www to with-www alitrix Linux - Server 3 07-24-2008 06:11 AM
"www::mechanize"-->www.cpan.org using this module login to secure website vvnkishore Linux - General 0 10-04-2007 01:14 AM
for www.cafeyurt.com and www.sohbetedin.net timsah Linux User Groups (LUG) 10 02-11-2006 02:20 PM
mod_rewrite, removing the www belorion Linux - General 5 08-05-2005 03:06 PM
Just bought www.helpwithlinux.net and www.helpwithwindows.com Whitehat General 15 05-08-2003 12:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration