Helpful Information
 
 
Category: Apache configuration
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

I'm currently unable to test it, sorry 'bout that, but I guess the script below comes close:
(and might give you the idea)


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

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:


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



RewriteEngine on
RewriteCond %{HTTP_HOST} !www.athyrium.ca [NC]
RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [R]

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

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?)

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

Or if your conf file has the SERVER_NAME setup NOT as the FQDN, you could just do



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:



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.

I use this:


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 (http://no-www.org/).










privacy (GDPR)