目的:如果存在,则导致浏览器重写为file-name.php;否则返回file-name.html -访问者是否输入了以下任意url:
-
http://mydomain.com/file-name
-
http://mydomain.com/file-name.html
-
http://mydomain.com/file-name.php
在根目录下的。htaccess文件中使用以下规则取得了良好的成功:
# REWRITE FILE URI TO file.php IF EXISTS
Options Indexes +FollowSymLinks +MultiViews
Options +ExecCGI
RewriteEngine on
RewriteBase /
# parse out basename, but remember the fact
RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
# rewrite to document.php if exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
然而,我已经安装了WP的根,与已有的网站,这些规则不再工作。
作用: file-name
被重写为file-name.html
或file-name.php
-以存在的文件为准。
不工作的地方: file-name.html
不重写为file-name.php
,即使没有file-name.html
和file-name.php
存在。另外,当没有file-name.php
但有file-name.html
时,file-name.php
不会被重写为file-name.html
。
整个。htaccess现在的样子:
# BEGIN WP MULTISITE RULES
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*.php)$ $1 [L]
RewriteRule . index.php [L]
# END WP MULTISITE RULES
# REWRITE FILE URI TO file.php IF EXISTS
Options Indexes +FollowSymLinks +MultiViews
Options +ExecCGI
RewriteEngine on
RewriteBase /
# parse out basename, but remember the fact
RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
# rewrite to document.phtml if exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
有什么建议吗?
快速概述告诉您,您的原始规则很可能永远不会被访问,因为WP规则应该拦截所有请求。
具有这些条件的RewriteRule ^ - [L]
行将中止重写任何已经存在的文件或文件夹,而RewriteRule . index.php [L]
行将拦截/重定向到index.php
的所有请求。
如果你把你的规则移到WordPress 1上面,它会再次工作。
要将不存在的。php文件的请求重写为。html文件,使用以下规则:
# rewrite non-existing .php file to existing .html
RewriteCond %{DOCUMENT_ROOT}/$1.php !-f
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+).php$ $1.html [L,PT]
把它放在规则下面(但是在WP上面)。该规则将检查.php文件是否存在,只有当.html文件存在时才会进行重写。如果两个文件都不可用,那么什么也不会发生。
请记住,因为这些检查和事实,规则是在重写链的顶部,这条规则将评估每一个请求。php文件(甚至WP页面),这可能会给非常繁忙的服务器带来额外的压力。理想情况下,您希望首先有适当的url,这样就不需要进行此类操作。