dongwen7371 2019-08-19 09:26
浏览 109

如何htaccess 301重定向 - 旧网站“.php”扩展到WordPress“/”没有冲突“wp-admin”通过404缺页

Currently, I have my htaccess on WordPress setup like this:

#I want to prevent any .php 301 redirects in wp-admin, wp-includes, wp-content
RewriteCond %{REQUEST_URI} !^(.*)?wp-login\.php(.*)$
RewriteCond %{REQUEST_URI} !^(.*)?wp-admin$
RewriteCond %{REQUEST_URI} !^/(wp-includes/.*|wp-admin/.*|wp-content/.*)$

#I used the standard WordPress htaccess along with the .php redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule (.*)\.php$ /$1 [R=301,L] #this does the 301 redirects with .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I am looking to 301 redirect my old .php site to WordPress, for example anything that has ".php" will be redirected to "/" without effecting wp-admin, wp-content, wp-includes original functionality.

wp-admin, wp-includes, wp-content - are dependent on .php and they can't be redirected or it will create a 404 error / missing page.

THE PHP FIX: I created a temporary fix with PHP and it's working perfectly, I added this to theme/function.php on the first line:

$link = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$admin_url = ["wp-admin", "wp-includes", "wp-content"];
$count = 0;
$admins = '';
foreach ($admin_url as $url) {
if (strpos($link, $url) !== false) {
    if($count === 0){
    $admins = "admins";
    $count = 1;
    }
}
}
if (strpos($link, '.php') !== false && $admins == '') {
wp_redirect( str_replace('.php', '/', $link) );
exit;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码