将 .php URL 重定向到没有扩展名的 URL [英] Redirect .php URLs to URLs without extension
问题描述
可能的重复:
使用 .htaccess 删除 .php 扩展名
如果可能,我想将我的所有网站 URL myurl/webpageexamples.php
重命名为其非扩展版本 myurl/webpageexamples
href="https://en.wikipedia.org/wiki/.htaccess" rel="nofollow noreferrer">.htaccess 并同时重定向来自 myurl/webpageexamples.php的所有流量code> 到新的
myurl/webpageexamples
.
I want to make all my website URLs, myurl/webpageexamples.php
, to be renamed to its non-extension version myurl/webpageexamples
, if possible in .htaccess and at the same time redirect all traffic from myurl/webpageexamples.php
to the new myurl/webpageexamples
.
我发现有几个可以从 PHP 重定向到非 PHP,例如:
I have found several to redirect from PHP to non-PHP like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ /$1 [L,R=301]
我有什么:
myurl/webpageexamples.php
我想要的:
myurl/webpageexamples
我想将 SEO 分数转移到新的 myurl/webpageexamples
.
I would like to have the SEO score transferred to new myurl/webpageexamples
.
推荐答案
如果实际请求是针对 php 页面,您要确保重定向,并在 URI 缺少 php 时进行内部重写:
You want to make sure you are redirecting if the actual request is for a php page, and internally rewriting when the URI is missing the php:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^ ]+).php
RewriteRule ^/?(.*).php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
这篇关于将 .php URL 重定向到没有扩展名的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!