skip to Main Content

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?

3

Answers


  1. 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]
    
    Login or Signup to reply.
  2. <?php
        $extension=end(explode(".",$_SERVER['PHP_SELF']));
        $arr=["png","jpg"];
        if(in_array(strtolower($extension), $arr)==""){
            header("location:https://www.google.com");
        } ?>
    
    Login or Signup to reply.
  3. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search