【问题标题】:Htaccess redirect seems not working with php file extension to seo friendly (no extension) urlHtaccess 重定向似乎无法将 php 文件扩展名用于 seo 友好(无扩展名)url
【发布时间】:2022-11-27 23:37:18
【问题描述】:

我网站的一个旧 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]

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

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

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

    【讨论】:

    • 看起来不错,但它也覆盖了此 URL https://example.com/admin/admin_attention?hello=world 的扩展名。当文件位于/admin 时,我不想重定向。
    • 好的,现在试试我更新的规则
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2015-01-25
    • 2021-09-09
    相关资源
    最近更新 更多