大网数据广告活动
服务器运维问答

Apache: Redirect all .html AND .php requests to folder + WordPress

· 52CE 我爱测

After a relaunch I have a bunch of dead backlinks pointing at URLs with .php or .html file-extensions. It would be nice to redirect them in bulk using .htaccess and the mod_rewrite module. The redirection of .html requests are wo…

问题原文

原帖链接 · schuggerleo · CC BY-SA 4.0

After a relaunch I have a bunch of dead backlinks pointing at URLs with .php or .html file-extensions. It would be nice to redirect them in bulk using .htaccess and the mod_rewrite module.

The redirection of .html requests are working fine, however, I can’t get the redirect of .php URLs to work. Please note that I use WordPress so there has to be an exemption of the /wp-admin/ folder.

This is what I've come up with so far, but it doesn't work for .php:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Redirect http requests to https
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.example.com/$1 [L]

# Redirect html
RewriteCond %{REQUEST_URI} \.html
RewriteRule ^(.*)\.html$ /$1 [L] 

# Redirect php NOT WORKING
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{REQUEST_URI} \.php
RewriteRule ^(.*)\.php$ /$1 [L] 

# Lines below come from WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Can you spot where I'm wrong? Or am I completely lost?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

# Redirect html
RewriteCond %{REQUEST_URI} \.html
RewriteRule ^(.*)\.html$ /$1 [L]

This isn't a "redirect", as in an "external 3xx redirect", that you appear to be suggesting. If you are seeing a "redirect" then either something else is doing that, or you are seeing a cached response. The directive above is an internal rewrite - the URL in the browser's address bar does not change (this would be bad for SEO).

The RewriteCond directive is also superfluous in this instance... it just means you are checking for .html on the requested URL twice.

If you want to remove the .html extension via a redirect then you just need something like:

RewriteRule ^(.*)\.html$ /$1 [R,L]

However, to remove the .php extension from the URL then you also need to avoid redirecting index.php (that the WordPress front-controller rewrites to), or only make sure you are checking the initial request, not the rewritten request. For example:

# Redirect php (avoiding "/wp-admin/" and "/index.php")
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteRule ^(.*)\.php$ /$1 [R,L]

To make sure you are only testing the initial request and not a rewritten request, then you can use a condition like:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

This simply checks that the REDIRECT_STATUS environment variable is empty - which it is on the initial request. After the first successful rewrite it gets set to "200" (as in 200 OK HTTP status).

Change the R to R=301 if this is intended to be a permanent redirect, but only once you have confirmed that it's working OK.

You should also avoid adding your own custom directives to the # BEGIN WordPress block as these could be overridden by WordPress itself. Instead these directives should be written before the # BEGIN WordPress block, together with your HTTP to HTTPS redirect (which isn't actually a redirect either - you need the R flag on the RewriteRule).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-07-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。