的.htaccess和robots.txt的重写URL如何重写、robots、htaccess、URL

2023-09-02 20:34:36 作者:仙女味的小可爱

下面是我的$ C $下的.htaccess

  RewriteEngine叙述上
重写规则(。*)的index.html
 

问题:当访问mydomain.com/robots.txt然后再次页面重定向到index.html的 要求:

 如果(URL包含的robots.txt)然后
    重定向到mydomain.com/robots.txt~~V
其他
    重定向到的index.html
 
50w一节课的Python爬虫工程师教你五分钟拿下Python爬虫

解决方案

试试这个:

  RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则(。*)的index.html
 

基本上,这两个的RewriteCond 告诉Apache重写URL的仅在所需的文件( -f )或目录( -d )不存在(即作为否定)

另外,如果你需要它只是为的robots.txt 您可以使用类似:

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

而不是两个的RewriteCond 以上。

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

解决方案

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.