Gjert
Gjert

Reputation: 1067

Force WWW when URL contains path using .htaccess

I'm having a problem with my URL and my sessions. I wish to have ALL website pages be forced to use www. As it looks like now, the website looks like this:

www.example.com into www.example.com

example.com into www.example.com

www.example.com/example/ into www.example.com/example/

example.com/example into example.com/example (this is what's wrong)

This is what my .htaccess file looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^wewent\.net
RewriteRule ^(.*)$ http://www.wewent.net/$1 [L,R=301,NC]
</IfModule>
# END WordPress

Because the URL does not redirect properly I get double up with sessions one for www and one for the website without. How can I prevent this the best way?

Upvotes: 0

Views: 39

Answers (1)

Panama Jack
Panama Jack

Reputation: 24468

It seems to look ok but one thing you should do is always put your other rules before the wordpress rules as a habit. When using wordpress it should generally be the last set of rules since it does all the routing. Now for the redirect, you should probably use 302temporary which will remove any current cache and verify that your redirects are working properly. Then you can change it to 301 for permanent once it's working correctly.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.wewent\.net [NC]
RewriteRule ^(.*)$ http://www.wewent.net/$1 [L,R=302,NC]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Upvotes: 2

Related Questions