Olesya M
Olesya M

Reputation: 31

how to make several redirection

Right now I am updating my web site and I want to change all links to non-extension links like example.com/test.php to example.com/test. THis solution I found I put in .htaccess file:

RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php

RewriteRule ^.+\.php$ /%1 [L,R=301]

But at the same time I need to make redirection for links what located in lower level of my web-site like example.com/level/test2.php to example.com/test2.php. What should I write in .htaccess to redirect my files from lower level to higher and remove .php extension at the same time?

Upvotes: 2

Views: 56

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You need to separate your groupings and backreference the 2nd one, not the entire thing:

RewriteCond %{THE_REQUEST} ^GET\ /([^/?]*/)*([^/?]+)\.php
RewriteRule ^.+\.php$ /%2 [L,R=301]

Essentially, /(([^/?]*/)*[^/?]+)\.php is changed to ([^/?]*/)*([^/?]+), then %1 is changed to %2 in the rule's target.

Upvotes: 1

Related Questions