1

I have an htaccess file that makes all requests to be handled by index.php:

RewriteEngine On
RewriteBase /

RewriteRule ^(.*)/?$ index.php?q=$1 [L]

I want the same behavior except for all requests to myfile.php (including extra query strings like myfile.php?somecontent=1). So all requests that contain myfile.php in the query strings are to be handled by the original file - myfile.php

how do I add such an exception?

2 Answers 2

1

You can add negative lookahead condition in this rule to skip certain know URI for this rule:

RewriteEngine On
RewriteBase /

RewriteRule ^(?!(?:index|myfile)\.php$)(.+?)/?$ index.php?q=$1 [L,QSA,NC]

(?!(?:index|myfile)\.php$) is negative lookahead assertion that skips this rule for /myfile.php and /index.php.

Sign up to request clarification or add additional context in comments.

Comments

1

You just need to add a RewriteCond before your RewriteRule

Simplest way:

RewriteCond %{REQUEST_URI} ! myfile.php 
RewriteRule ^(.*)/?$ index.php?q=$1 [L]

There is also a more generic way to exclude all existing files and directory:

RewriteCond %{REQUEST_FILENAME} ! -f
RewriteCond %{REQUEST_FILENAME} ! -d
RewriteRule ^(.*)/?$ index.php?q=$1 [L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.