Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite rule if condition is not matched

How may I perform a rule if the URL is NOT matching the path "forums"?

For example:

RewriteCond IF URL IS NOT forums RewriteRule !\.(js|gif|css|jpg|png)$ %{DOCUMENT_ROOT}/index.php [L] 
like image 209
jcoahd Avatar asked Mar 18 '11 12:03

jcoahd


People also ask

What does rewrite rule do?

RewriteRule defines a particular rule. The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article. The second string after RewriteRule defines the new URL.

What is rewrite cond 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 Apache rewrite rule?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.


1 Answers

Apache's RewriteCond as well as the RewriteRule directive support the exclamation mark to specify a non-matching pattern:

You can prefix the pattern string with a '!' character (exclamation mark) to specify a non-matching pattern.

This should work:

RewriteCond %{REQUEST_URI} !^/forums.* RewriteRule !\.(js|gif|css|jpg|png)$ /index.php [L] 

--> redirect all requests not beginning with forums and not ending with the listed suffices to index.php

like image 187
marapet Avatar answered Sep 18 '22 17:09

marapet