search

Home  >  Q&A  >  body text

Htaccess redirect doesn't seem to work with php file extensions for seo friendly (no extension) urls

An old URL structure of my website is as follows:

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

After rebuilding the URL, the URL will look like this:

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

Now, when a user/visitor uses the old URL https://example.com/login.php, I want to redirect to https://example.com/login page. what should I do?

FYI, currently, my .htaccess file has the following content:

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=&id= [L,QSA]

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

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

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

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

P粉724737511P粉724737511482 days ago679

reply all(1)I'll reply

  • P粉464208937

    P粉4642089372023-09-14 09:29:25

    You can remove the .php extension by inserting this rule below the RewriteBase / line:

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

    reply
    0
  • Cancelreply