Timur Gafforov
Timur Gafforov

Reputation: 761

htaccess: Handle all requests by index.php except for particular page

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?

Upvotes: 1

Views: 44

Answers (2)

silmaril
silmaril

Reputation: 443

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]

Upvotes: 1

anubhava
anubhava

Reputation: 785471

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.

Upvotes: 1

Related Questions