.htaccess and robots.txt rewrite url how to

和自甴很熟 提交于 2019-12-10 11:51:51

问题


Here is my code for .htaccess

RewriteEngine on
RewriteRule (.*) index.html

Problem : when visit mydomain.com/robots.txt then page again redirect to index.html
Required :

if(url contain robots.txt) Then   
    redirect to mydomain.com/robots.txt   
else   
    redirect to index.html

回答1:


Try this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html

Basically, those two RewriteCond tell apache to rewrite the URL only if the requested file (-f) or directory (-d) doesn't exists (the ! serves as a negation).

Alternatively, if you need it just for robots.txt you can use something like:

RewriteCond %{REQUEST_URI} !^/robots.txt$

instead of the two RewriteCond above.



来源:https://stackoverflow.com/questions/7930866/htaccess-and-robots-txt-rewrite-url-how-to

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