Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to put an exception to RewriteRule in .htaccess

I am redirecting all requests like so:

RewriteRule ^sitemap.xml$ sitemap.php?/ [QSA,L]

# the line below is the one I'm having trouble with

RewriteCond %{REQUEST_URI}  !^market-reports$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?section=$1 [QSA,L]

All my incoming links are meant to go to index.php, as you can see. But now I want to stop one from going there. I've never written my own RewriteCond before, so I'm a little unsure if what I am doing is correct.

Basically what I'm trying to say is: "If incoming URL is a file, directory or /market-reports/ do nothing. Otherwise send on the URL to index.php?section="

What am I doing wrong? Thanks

like image 728
alex Avatar asked Feb 09 '10 02:02

alex


People also ask

What is RewriteRule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is RewriteCond and RewriteRule?

There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.


1 Answers

So you just need to ignore http://yourdomain.com/market-reports (in addition to files/directories?). You should be fine with:

RewriteCond %{REQUEST_URI} !^/market-reports/?$

This will (not) match "http://yourdomain.com/market-reports" as well as "http://yourdomain.com/market-reports/" as the question mark "?", in the Perl Compatible Regular Expression vocabulary that mod_rewrite uses, makes the match optional (a wildcard) before the end of the string anchor, which is represented with the literal dollar sign "$".

The "^" symbol acts as an anchor matching the beginning of the string and the "!" negates the match, so that any string URL that does not match the rest of the expression will be rewritten to the other specified rules. See mod_rewrite regex vocabulary

like image 78
Owen Avatar answered Oct 25 '22 15:10

Owen