Htaccess重定向似乎不工作与php文件扩展名到seo友好(无扩展名)url

qyyhg6bp  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(165)

我的网站的一个旧的URL结构是这样的:

https://example.com/login.php
    or, 
    https://example.com/login.php?redirect=https://example.com
    or,
    https://example.com/register.php

重建URL后,URL如下所示:

https://example.com/login
    or, 
    https://example.com/login?redirect=https://example.com
    or,
    https://example.com/register

现在,当用户/访客使用旧的URL https://example.com/login.php时,我想重定向到https://example.com/login页面。我该怎么做呢?
仅供参考,目前,我的.htaccess文件包含以下内容:

AddDefaultCharset UTF-8

ErrorDocument 404 /error-404/

##
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]

## Avoid rewriting rules for the admin section
RewriteRule ^(admin|resources)($|/) - [L]

## Set Ajax Request File
RewriteRule ^kahuk-ajax.php?$ kahuk-ajax.php? [L,QSA]

## Set controller with id
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA]

## Set controller with slug
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA]

## For paging
RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA]

## Set controller for only one parameter
RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]

## Set home page
RewriteRule ^/?$ index.php?con=home [L]
h4cxqtbf

h4cxqtbf1#

您可以在RewriteBase /行的正下方插入此规则,以移除.php扩展名:

# To externally redirect /afile.php to /afile
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule !^admin/ /%1 [R=301,NE,L,NC]

相关问题