Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritingfriendly-url

Prevent htaccess form redirecting to directory which is in url


I have three lines in htaccess file:

RewriteEngine On
RewriteRule ^fruits/ /content/fruits.php
RewriteRule ^fruits/apple/ /content/apple.php

and how I can prevent redirecting to fruits.php from https://www.exmapledomain.com/fruits/apple/?

Thank you in advance for your response.


Solution

  • Make sure your htaccess file is present in root along with content folder(not inside content folder). With your shown samples, please have htaccess file in following manner. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine On
    RewriteRule ^(fruits)/?$ /content/$1.php [NC,L]
    RewriteRule ^fruits/(apple)/?$ /content/$1.php [NC,L]
    


    Generic solution: In case fruit and apple are just examples and you want to have it Generic for any string then have following Rules. Make sure you use either above OR following Rules one at a time only.

    RewriteEngine On
    RewriteRule ^([\w-]+)/?$ /content/$1.php [L]
    RewriteRule ^([^/]*)/([^/]*)/?$ /content/$1.php [L]