#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2

    How to redirect www. to non www.?


    Well at this point I am trying to redirect all www paths to non www paths so that my router/dispatcher wont mess up with client users having subdomains. As I am designing a software, I cannot just use specific url with an actual domain name, it needs to be a generic solution regardless of what your domain name and extension is. I've read a few articles, including this one on stackoverflow.com, which offers this approach:
    http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www

    PHP Code:
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
    The problem is, well, it does not take care of subpaths. Lets say I have four sites located at:

    PHP Code:
    http://www.domain.com/index.php
    http://subdomain.domain.com/index.php
    http://www.domain.com/scriptpath/index.php
    http://subdomain.domain.com/scriptpath/index.php 
    The first two will work out just fine, they are being directed to http://domain.com/index.php and http://subdomain.domain.com/index.php. The third one, however, does not work at all. It is redirected to http://domain.com/index.php rather than http://domain.com/scriptpath/index.php, the scriptpath is left missing! The fourth one apparently aint functioning as expected either...

    Is there a way anyone can fix the design of that .htaccess I posted above so that it takes care of both regular domains, subdomains and domains with subpaths? Please help...
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,071
    Rep Power
    9398
    It does depend on where these rules are, but a general-purpose solution would be
    Code:
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    Originally Posted by requinix
    It does depend on where these rules are, but a general-purpose solution would be
    Code:
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
    Well this does not seem to work out, it directs everything to index.php...

    PHP Code:
    http://domain.com/scriptpath/index.php 

    I think the reason is that I am using url rewrite in my script already so that everything is forwarded to this url:
    PHP Code:
    RewriteRule ^(.*)$ index.php [QSA,L
    So yeah, there is a front controller sitting at index.php to handle requests and then delegates to app controllers. With your rewrite rule, the urls all appear to be domain.com/scriptpath/index.php. Is there a way to fix this? Thx.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,071
    Rep Power
    9398
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    Originally Posted by requinix
    Put that after the no-www one.
    Cool, it works out nicely, thank you so much. I was putting the non www. one before the url rewrite rule earlier, guess that was the problem.


    And if you dont mind me asking, is there a way to only redirect php files within the same directory to the index page? I mean, I have to add this line to .htaccess to get javascript, css and image/media files to work properly:

    PHP Code:
    RewriteCond %{REQUEST_URI} !(\.js|\.css|\.gif|\.jpg|\.png)$ [NC
    But I personally dont like it, its too cumbersome and more importantly, there's still no way to distinguish among php files.
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    This should do it:
    Code:
    RewriteRule ^/?[^/]+\.php$ index.php [QSA,L]
    Alternatively, you could shorten the condition to this:
    Code:
    RewriteCond %{REQUEST_URI} !\.(js|css|gif|jpg|png)$ [NC]
    Do you really have some of those file types in the web-root directory?
    Last edited by Kravvitz; December 28th, 2012 at 10:19 AM.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    Thanks for your reply. Well, the first one does not really work, it shows me a blank page. The second approach does work though, so its a bit of improvement from my old style.

IMN logo majestic logo threadwatch logo seochat tools logo