Magnotta
Magnotta

Reputation: 941

redirect and rewrite url using .htaccess

Recently I was thinking of making some seo friendly urls by rewrite and redirecting urls.

the rule which I wrote is working fine for this type of url like xyz.com/abc.php this automatically got converted into xyz.com/abc now I need help with this type of urls abc.com/study.php/abc/123 which I want to redirect on abc.com/study/abc/123

my .htaccess file is

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]

EDIT.

It was working fine now I have changed my mind that instead of removing .php extension from all files I want to remove .php extension specifically from 2 files i.e a.php and b.php.

Upvotes: 1

Views: 623

Answers (1)

anubhava
anubhava

Reputation: 785156

You can tweak your existing rules to support this PATH_INFO as well like this:

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+(\S+?)\.php(/\S*)?\sHTTP [NC]
RewriteRule ^ /%1%2 [L,R=301,NE]

# check to see if the request is for a PHP file:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.+?)(/.*)?$ /$1.php$2 [L]

Upvotes: 1

Related Questions