Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Apache mod_rewrites & URL formatting

    G'day all.

    I am trying to find a script for Apache which will display the URL of my web site as: htp://athyrium.ca, WITHOUT the leading "www". I know it can be done, because I have seen many sites which use this convention. Currently, it displays WITH the "www".

    One of the CodingForums Senior Members kindly directed me in this direction, suggesting I check here, and that I check Apache's "mod_rewrite" docs for further direction.

    This is good, and I am following through, but perhaps somebody here with experience can help me cut to the chase and suggest specific mod_rewrite examples for me to try out?

    I have a 2nd machine at home running Linux with Apache installed which I can configure and mess with. I plan to work with my web host to make the changes.

    Thanks!
    Russell

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm currently unable to test it, sorry 'bout that, but I guess the script below comes close:
    (and might give you the idea)

    Code:
    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} ^www\.athyrium\.ca [NC]
    RewriteRule www\.athyrium\.ca athyrium\.ca [R]
    Explanation:
    RewriteEngine on this turns the RewriteEngine on
    RewriteCond the condition is:
    %{SCRIPT_FILENAME} the filename
    ^ starts with
    www\.athyrium\.ca www.athyrium.ca (your address including www)
    [NC] No Case; case insensetive
    RewriteRule what to do
    www\.athyrium\.ca replace www.athyrium.ca by
    athyrium\.ca athyrium.ca
    [R] sends a redirect code to the browser

    Well, I hope it's (quite) clear to you and it helps you out.

    Mzzl, Chris

  • #3
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So you want to get rid of a FQDN? Hmm, odd, best practice is to always go for the FQDN...ah well. The problem with Chris's code is that it will only work on direct requests of the domain name, not subsequent pages or any URI with a page or additional file pointer.

    Just use this:
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST}  !www.athyrium.ca [NC]
    RewriteRule ^.*$ http://athyrium.ca%{REQUEST_URI} [R]
    Or if your conf file has the SERVER_NAME setup NOT as the FQDN, you could just do

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST}  !www.athyrium.ca [NC]
    RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R]
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #4
    New to the CF scene
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, thanks, guys. Great tips. I'll pass these along and see what my hosts think of them.

    Regarding the FQDN thing, would you anticipate problems as a result? If so, why would large orgs like javafile.com use the convention?

    And I have checked their site again, and they do display it as both with or without, depending on what gets typed into the location bar. Interesting.

    Thanks again!
    Regards,
    Russell Collier

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Hi,
    if you or your host have control over your DNS then they can just add an entry to the DNS records so that http: // yourdomain.com points to the right place anyway & then mod_rewite is a little OTT
    (of course this has nothing to do with the fact that I cant get a handle on mod_rewrite stuff but it does seem the easier route?)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    New to the CF scene
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent suggestions, all. I'll talk with my hosts and see if they'll run it. I'll also play around with it at home until I get a feel for it myself.

    Thanks again!

    Russell

  • #7
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Feyd
    Or if your conf file has the SERVER_NAME setup NOT as the FQDN, you could just do

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST}  !www.athyrium.ca [NC]
    RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R]
    I don't think that's right. If the HTTP_HOST for the incoming request is www.athyrium.ca, the idea (I think) is to redirect the request to athyrium.ca -- so he'd want the lines to read instead:

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST}  =www.athyrium.ca [NC]
    RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R]
    The exclamation point would say that the http://athyrium.ca redirect occurs only if the incoming request is NOT www.athyrium.ca. By using the equals sign we're saying the redirect is to occur only if the incoming request is www.athyrium.ca.

    Also, I think you want the [R] to read [R=301] so that a permanent redirect is signaled to the linking page (rather than the 302 temporary redirect that I believe is the default). Also, I think you want it to read [L,R=301] instead of just [R], so that the redirect occurs immediately. Then if there are any other redirect rules to be applied, they'll be applied to the redirected, correct athyrium.ca incoming request.

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    I use this:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
    Works like a charm. Got it from no-www.org.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •