开发者

.htaccess permanent redirect to www

开发者 https://www.devze.com 2023-01-24 11:11 出处:网络
I have a wordpress instalation that is not redirecting urls not starting with www. Example: http://examp开发者_StackOverflowle.com/dir/ doesn\'t send to http://www.example.com/dir/ instead goes to ht

I have a wordpress instalation that is not redirecting urls not starting with www.

Example: http://examp开发者_StackOverflowle.com/dir/ doesn't send to http://www.example.com/dir/ instead goes to http://example.com/

How do I change the .htaccess below to always redirect to the page either using www or not?

# BEGIN WordPress
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

# END WordPress


You should not have to modify your .htaccess; actually I would not recommend it.

Sounds like you simply have your values for WordPress address and Site address set correctly in your admin console in the Settings > General section:

.htaccess permanent redirect to www

Another option is to add the following 2 lines somewhere within your /wp-config.php file but before require_once(ABSPATH . 'wp-settings.php'); (and be sure not to use a trailing slash):

define('WP_HOME',"http://www.example.com/dev");
define('WP_SITEURL',"http://www.example.com/dev");

I prefer this approach because I like to keep my configuration options in code whenever possible since it is easier to maintain that way than configuration options stored in the database.

Hope this helps.

-Mike

P.S. Next time you have a WordPress question, consider asking over at StackOverflow's sister site WordPress Answers? Lots of WordPress enthusiasts over the to help out.


I got the correct answer from webmasters.stackexchange.com. This works perfectly! Thanks Ruel for pointing me to webmasters.stackexchange.

Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


More like a question for http://webmasters.stackexchange.com

Well anyway, if you are using cPanel, you can do a wildcard redirect from Redirects under Domains category.

But if not, try this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]


Works perfectly. Put this at the top of wp-config.php file:

define('WP_HOME',"http://www.example.com/dev");
define('WP_SITEURL',"http://www.example.com/dev");
0

精彩评论

暂无评论...
验证码 换一张
取 消