简体   繁体   中英

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

I am using that code in htaccess file but not working

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
  2. You are causing a redirect loop so you need to not redirect /index

    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]

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