简体   繁体   English

.htaccess 301将子域以及所有文件和文件夹重定向到根目录

[英].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 我正在尝试将所有内容从www.example.com重定向到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. 以下方法可以按预期的方向重定向www.example.com,但对其他文件或文件夹的请求返回302。这是Wordpress Multisite安装,但此时仅映射了一个域。

<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). 您需要所有路由规则(例如wordpress的东西) 之前添加重定向规则。 You are also missing a slash in your target and the HTTP_HOST does not contain any path info, just the hostname. 您还缺少一个斜杠你的目标和HTTP_HOST 包含任何路径信息,只是主机名。 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. 需要直接在htaccess文件顶部的RewriteEngine On下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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