Marcos Vinicios
Marcos Vinicios

Reputation: 23

Redirect file url to another page in .htaccess

I need to redirect all php files from end of urls to a specif dir like:

http://example.com/dir/file.php 
http://example.com/dir/folder/file.php 
http://example.com/dir/folder/folder2/file.php

to

http://example.com/dir2/file.php

my code is

RewriteRule ^(.*).php$ http://example.com/dir2/$1.php [R=301,L]

but I'm getting

http://example.com/dir2//dir/file.php

Upvotes: 2

Views: 47

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following :

RewriteRule ^.*/?([^/.]+).php$ http://example.com/dir2/$1.php [R=301,L]

This will just capture /file.php in the regex and not the directory.

Upvotes: 1

Related Questions