user392406
user392406

Reputation: 1323

(curiosity) .htaccess RewriteCond directive

I want to know what following code is doing in .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_URI} !(swf|thumbs|index.php|template.php)
RewriteRule ^([^/\.]+).php?$ template.php?cat=$1 [L,QSA]

Thanks in advance....

Upvotes: 1

Views: 1723

Answers (3)

KingCrunch
KingCrunch

Reputation: 132011

If the request uri does not contain "swf", "thumbs" and so on

RewriteCond %{REQUEST_URI} !(swf|thumbs|index.php|template.php)

make /template.php?cat=etc out of /etc.php

RewriteRule ^([^/\.]+).php?$ template.php?cat=$1 [L,QSA]

L = "last rule" and QSA = append any existing query string to the newly created target uri.

Upvotes: 1

Sean
Sean

Reputation: 1286

To answer the question about what the regexp actually means (as per my comment on the original answer above):

Each part in order:

^ - start of line

() - the grouping that $1 represents

[^/\.] - any character that is not / or a literal .

+ - more than one of the above character class

.php - obvious (though the . should be escaped, so it should be \.php)

? - unescaped in a regexp means 0 or 1 of the previous character

$ - end of line.

You'd probably be best off reading some regexp tutorials such as: http://www.regular-expressions.info/tutorial.html

Upvotes: 4

James C
James C

Reputation: 14159

The first line turns on the rewrite engine

The second line says "if the request uri doesn't contain swf or thumbs or index.php...

The third line will only be executed if line 2 was true (i.e. the URL didn't contain any of those strings). It will rewrite a URL like /123.php? to template.php?cat=123. The rewrite is internal so the user will just see the /123.php in their browser window.

Upvotes: 0

Related Questions