Using .htaccess to redirect domain.co.uk/index.html to www.domain.co.uk

这一生的挚爱 提交于 2019-12-11 07:45:40

问题


I have noticed that search engines have been crawling both the domain.co.uk and www.domain.co.uk versions of a web site that I've recently developed. Using .htaccess I have been able to setup http 301 redirects so that:

  • http://domain.co.uk is redirected to http://www.domain.co.uk

and

  • http://www.domain.co.uk/index.html is redirected to http://www.domain.co.uk

However:

  • http://domain.co.uk/index.html does not get redirected to http://www.domain.co.uk as I would expect. Instead the redirect goes to: http://www.domain.co.uk/http://www.domain.co.uk/

The contents of my .htaccess are (obviously I have replaced my domain to simplify matters):

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.co.uk [nc]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,nc]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.co.uk/$1 [R=301,L]

I know I am probably missing something obvious but please could someone help me get the final redirect to http://www.domain.co.uk working as expected?


回答1:


The first part of your rule set is working fine, the missing www. is added correctly. For the second part you only need a simple rule to remove the index.html without using any additional condition:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,NC]
RewriteRule ^index.html$ http://www.domain.co.uk/ [R=301,L]

I think this should do the trick.



来源:https://stackoverflow.com/questions/5607001/using-htaccess-to-redirect-domain-co-uk-index-html-to-www-domain-co-uk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!