简体   繁体   中英

.htaccess 301 redirect subdomain and all files and folders to root

I have been beating my head up against a wall for a day trying to figure this out. If you know how to do this and can help, it would be greatly appreciated.

I am trying to 301 redirect everything from www.example.com to the root domain at example.com

The following works to redirect www.example.com as intended, but requests for other files or folders are returning a 302. This is a Wordpress Multisite install, but it only has one domain mapped at this time.

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

# BEGIN WordPress
Options +FollowSymLinks 
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

# END WordPress

# Link re-directs from old website.

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

# Other

php_value memory_limit 500M

Can anyone help?? Please and thank you.

YOu need to add your redirect rule before all of your routing rules (eg wordpress stuff). You are also missing a slash in your target and the HTTP_HOST does not contain any path info, just the hostname. So this:

# Link re-directs from old website.

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

needs to go right under the RewriteEngine On at the very top of the htaccess file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM