将页面重定向到.htaccess中的索引

时间:2017-06-08 15:07:13

标签: .htaccess redirect

我想将页面重定向到index.php。当我点击某个链接时,它会重定向到http://example.com/article.php之类的尚未存在的内容。由于它不存在,我想重定向到index.php。

我尝试过类似的事情:

RewriteRule ^article.\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [R=301]

此代码重定向到index.php,但网站上的每个链接都不会重定向到article.php

1 个答案:

答案 0 :(得分:1)

您可以改用此规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article\.php$ /index.php? [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-f确保仅当index.php不是系统中的有效文件时才会发生重定向。