arun kamboj
arun kamboj

Reputation: 1315

How to redirect user to another location from particular folder if there is no extension(like .png,.jpg) exist at the end of url

I want to redirect all url of particular folder to another location if there is no extension exist at the end of url.

I have "Test" folder in the server.

my url are like

www.example.com/Test/abc
www.example.com/Test/abc1
www.example.com/Test/abc2

So i want that when user click on these url then it will be redirect to the index page(www.example.com/index.php)

But if url have extension at the end then it should not be redirect.

like www.example.com/Test/logo.jpg, www.example.com/Test/p1.mp3

I have tried to do it using .htaccess file. and i am able to redirect to index page but my problem is that when user click on any image url or mp3 url then it will also redirect user to index page.

here is my .htaccess file

<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch 
RewriteEngine On 
Redirect /Test http://example.com/index.php?id=
</IfModule>

It redirect me to the following url

http://example.com/index.php?id=abc (if i clicked on http://example.com/Test/abc)

Any Idea?

Upvotes: 0

Views: 67

Answers (3)

Vizjerei
Vizjerei

Reputation: 1030

Well in .htaccess you could do something like:

RewriteEngine On
RewriteRule ^Test/(.*)\.jpg$ /index.php?id=$1.jpg [L]
RewriteRule ^Test/(.*)\.mp3$ /index.php?id=$1.mp3 [L]

If you want a 301 redirect:

RewriteCond %{REQUEST_URI} \.(jpg|png|mp3)$ [NC]
RewriteRule ^Test/(.*)$ http://example.com/index.php?id=$1 [R=301,L]

Upvotes: 1

M.Bains
M.Bains

Reputation: 405

Could you please try this? I have not tested it but it should work for you.

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteRule \.(js|css|ico|gif|jpg|png|xml|html|mp3|ttf|eot)$ - [NC,L]
RewriteRule ^Test/(.*)$ /index.php [L]
</IfModule>

Upvotes: 0

Parvej Alam
Parvej Alam

Reputation: 258

<?php
    $extension=end(explode(".",$_SERVER['PHP_SELF']));
    $arr=["png","jpg"];
    if(in_array(strtolower($extension), $arr)==""){
        header("location:https://www.google.com");
    } ?>

Upvotes: 0

Related Questions