简体   繁体   English

在php项目中的htaccess重定向中需要帮助

[英]Need help in htaccess redirect in a php project

I want to redirect mydomain.com and www.mydomain.com to http://www.mydomain.com/index 我想将mydomain.com和www.mydomain.com重定向到http://www.mydomain.com/index

I am using that code in htaccess file but not working 我正在htaccess文件中使用该代码,但无法正常工作

RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteCond %{HTTP_HOST} ^www.mydomain.com

RewriteRule ^(.*)$ http://www.mydomain.com/index [r=301,L]

I have also try 我也尝试过

RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/index [r=301,L]

RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/index [r=301,L]

But not working so Please help me 但无法正常工作,请帮助我

You're missing 2 things. 您错过了两件事。

  1. You can use the [OR] flag in your conditions, or simply combine the regex 您可以在您的条件中使用[OR]标志,或仅将正则表达式组合在一起
  2. You are causing a redirect loop so you need to not redirect /index 您正在引起重定向循环,所以你需要重定向/index

    RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC] RewriteCond %{REQUEST_URI} !^/index RewriteRule ^(.*)$ http://www.mydomain.com/index [r=301,L] RewriteCond%{HTTP_HOST} ^(www。)?mydomain.com $ [NC] RewriteCond%{REQUEST_URI}!^ / index RewriteRule ^(。*)$ http://www.mydomain.com/index [r = 301, L]

Or if you only want to redirect requests for the root : 或者,如果您只想重定向对根的请求:

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteRule ^$ http://www.mydomain.com/index [r=301,L]

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

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