skip to Main Content

I am trying to make .htaccess rule not affect other file url

example
my .htaccess rule is

RewriteEngine On
RewriteRule ^([^/]*)$ /tr/hp.php?q=$1 [L]

my site url is

mydomain.com/keywords

everything working good on keywords but when I try to open robots.txt

mydomain.com/robots.txt

OR

mydomain.com/images.jpg

any other file url

redirect on /tr/hp.php?q=filename

which .htaccess Rewrite Rule works on both?

2

Answers


  1. Try :

    RewriteEngine On
    #--exclude real directories
    RewriteCond %{REQUEST_FILENAME} !-d
    #--and files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ /tr/hp.php?q=$1 [L]
    
    Login or Signup to reply.
  2. You also have to prevent matching any request pattern that carries a dot in it:

    RewriteEngine On
    RewriteRule ^([^/.]*)$ /tr/hp.php?q=$1 [L]
    

    Certainly it is possible to further refine this pattern.


    Alternatively some people like to prevent rewriting requests to files or folders that physically exist in the file system using RewriteCond:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    

    But that is something you have to decide upon. This does not help for example if resources like robots.txt are delivered in a dynamic manner…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search