Htaccess: remove www and keep friendly urls

Hello,

I have this typical htaccess rule for wordpress:


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Now, I also would like to get rid of the “www” part of the url. I have found this code:


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

But I don’t know how to integrate it in the first rules I posted.

Can someone help?

Cheers.

:slight_smile:

why you want to remove www from your url? Its have no drawbacks.

Yes, it can have SEO-wise. :slight_smile:

rh,

First, NEVER include an <IfModule> block in your .htaccess as it increases the load on the server significantly. (Note: WP does that to prevent causing websites to fail when the webmaster is an idiot - and mod_rewrite has not been enabled. In other words, it’s a CYA to prevent annoying messages from the ID-Ten-T types out there.)

Second, RewriteBase is a directive which does NOTHING for you (because it’s designed to undo a mod_alias redirection - which you don’t have).

Third, where did the first RewriteRule come from? That’s not WP code and is ridiculous (eliminate that, too).

Fourth, I’ve long criticized the . in the second RewriteRule as that REQUIRES at least one character in the URI. Make that . optional with a ?.

Finally, your www question: Fine, but it needs to be handled before the WP code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]
# you already have (.*) captured by the {REQUEST_URI} variable so use that instead!

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php [L]

Regards,

DK

DK: tank you so much. Why does DP issue such instead of your version?

rh,

Sorry, I thought I’d addressed that above with the quoted paragraph. To restate, a software company should never cause a server to go down for the inclusion of code which is not universally accepted. This is the case for mod_rewrite because (1) mod_rewrite is not part of the Apache core, (2) Apache does not enable mod_rewrite by default and some people will try to use mod_rewrite code on an IIS server (OMG!). At this point, only a poor host would not allow mod_rewrite on an Apache server but that’s simply my opinion (and something I check before signing up with a host). So, IMHO, WP (and other CMS creators) merely add the <IfModule> wrapper to protect themselves from ignorant webmasters as referenced by my Standard Rant #4:

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

I get upset when a webmaster fails to act like one and, as a former Team Leader here, I got tired of repeating the same advice to the point of Carpal Tunnel Syndrome and needed to establish “Standard Rants” to allow AutoCorrect to convert an abbreviation to a full paragraph for each of several “common errors” which were “button pushers” for me. Are you glad I didn’t start with that in my first response? :slight_smile:

Regards,

DK

Hello dklynn, thanks a lot for your helpa nd your answers!

Unfortunately, the .htaccess code you gave me doesn’t work. There is a redirection problem and firefox triggers an error message. I made sure that “domain.com” is exactly what my real domain is, so I’m not sure what could go wrong. Are ok to help me sort it out?

I was also wondering, is there a way to make the “without www.” part domain agnostic?

Cheers. :slight_smile:

rh,

Agnostic? How about optional?

RewriteEngine on
# force non-www domain name usage
RewriteCond %{HTTP_HOST} ^www\\.domain\\.com$ [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]
# you already have (.*) captured by the {REQUEST_URI} variable so use that instead!

# WP code (modified)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php [L]

You never specified what the problem was so let me explain, line-by-line:

[COLOR="#0000FF"]Ensure mod_rewrite is not in "comment mode"[/COLOR]
RewriteEngine on

# force non-www domain name usage
[COLOR="#0000FF"]If the domain name is not exactly www.domain.com (case insensitive)[/COLOR]
RewriteCond %{HTTP_HOST} ^www\\.domain\\.com$ [NC]
[COLOR="#0000FF"]Redirect every request to the non-www domain and display the new URL[/COLOR]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]
# you already have (.*) captured by the {REQUEST_URI} variable so use that instead!

# WP code (modified)
[COLOR="#0000FF"]If the file requested does not exist (protects against redirecting css, js, images, etc as well as index.php)[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
[COLOR="#0000FF"]AND it does not exist as a directory[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-d
[COLOR="#0000FF"]Redirect the request to index.php (WP's handler) withOUT displaying the new URI[/COLOR]
RewriteRule .? /index.php [L]

Since there is nothing that can go wrong (except the lack of index.php file), I’m left to wonder about the problem you’ve had. Please give more information.

Regards,

DK

Hello dklynn. :slight_smile:

When I use your code, my browser (firefox) connects to the site, the “waiting wheel” spins for a moment, then I get a firefox error message telling me that it has detected that the site wasn’t redirecting coorectly, and that it may be due to cookies. As soon as I put WP code back on, it works again. I checked several times, no issues with the way I wrote my domain name.

Thanks again for your precious help :slight_smile:

rh,

Look at your WP manager and eliminate those configurations which are requiring the www. subdomain. I’ve been helping someone with a similar problem with vBulletin and the admincp there is FULL of www. redirections which have affected the entire bulletin board (including the login). The code above is correct, just don’t let the configuration files to overrule the mod_rewrite.

Regards,

DK

:tup: