删除了.php扩展名,但现在它说页面不存在

时间:2019-02-01 21:35:29

标签: php wordpress .htaccess

我可以从文件中删除.php,但是现在当我转到不存在的页面时(当目标文件实际上位于WordPress之外的服务器上时,这将强制WordPress 404页面)。我认为问题可能在于它在具有WordPress的服务器上,而这些文件位于word press之外。 WordPress已经有重写规则,也许与我对其他文件的重写规则相冲突?

.htaccess文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Remove .php extension on files outside of wordPress
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]

# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

更新-完整的.htaccess

# BEGIN WP Hide & Security Enhancer
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 
#WriteCheckString:1548706048_68906
RewriteRule .* - [E=HTTP_MOD_REWRITE:On]

RewriteRule ^site(.*) /wp-login.php$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-login.php /index.php?wph-throw-404 [L]


RewriteCond %{REQUEST_URI} /enter$
RewriteRule ^(.*)$ /enter/ [R=301,L]
RewriteRule ^enter(.*) /wp-admin$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-admin(.+) /index.php?wph-throw-404 [L]

</IfModule> 

# END WP Hide & Security Enhancer




# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作

#remove all PHP extensions from the url
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301] #end and redirect


# Force trailing slash ( probably no longer needed, because `/$1/` )
#RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule .*[^/]$ $0/ [L,R=301]

#add .php back in and continue.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php  #no last flag as we can now continue to WP with our nice php extensions back on

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

我假设这些重写规则是正确的。我将不得不查找它们,因为几年来我没有与Rewrites进行大量合作。基本上

  • 首先删除所有php扩展名并重定向(如果存在)
  • 在重定向(或没有.php的请求)上,将php扩展名重新添加到已被删除的那些url中,因此为了安全起见,您可能需要%{REQUEST_FILENAME} !-d
  • 正常进行

按照一般的经验法则,最好先进行重定向。这样,您可以以最少的工作量进行重定向。重定向就像更改网址,然后将其放入浏览器中并再次运行它(即一个全新的请求)。

执行此操作的另一种方法是将所有请求重定向到某个文件夹,该文件夹到您拥有路由器的企业的index.php。

例如这个(我作为例子)

https://github.com/ArtisticPhoenix/MISC/tree/master/Router

然后您的网址可以是这样

http://yourdomain.com/somedir/index.php/controller/method/...args
#or without the index.php
http://yourdomain.com/somedir/controller/method/...args

然后在HTACCESS中,您要做的就是删除index.php

 #remove index.php and redirect
 RewriteRule somedir/index.php/(.+)/?$ /somedir/$1/ [L,R=301] #end and

 #add index.php back in
 RewriteRule somedir/(.+)/?$ /somedir/index.php/$1/ [L] 

 //wordpress stuff

关于路由器的好处是,您无需触摸Htaccess,只需输入一个文件夹名称即可简化操作。

使用路由器和示例文件:

 http://yourdomain.com/somedir/user/login

应转到Controllers/user.php方法login