Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to NOT match a word in mod_rewrite

Please help, I'm going crazy!

RewriteRule ^([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]

This is my current code. Sometimes people will visit mysite.com/search, other times they will visit mysite.com/boris/search and I detect a user with an empty($_GET['id']) check.

However I am creating another search, mysite.com/products/search which leads to products_search.php

I need my original RewriteRule to match any user EXCEPT the word 'products'.

I have tried so many combinations.

RewriteRule ^(!products&[a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]

I'm not very good with regex/mod_rewrite but I something like the above should work? I just need an AND operator as clearly & doesn't work, but I can't find one!

Many thanks in advance.

like image 648
John Avatar asked Oct 21 '10 11:10

John


People also ask

What is $1 in Apache rewrite rule?

The $1 is basically the captured contents of everything from the start and the end of the string. In other words, $1 = (. *) .

What does Mod rewrite do?

mod_rewrite lets you create all sorts of rules for manipulating URLs. For example, you can insert values pulled from the requested URL into the new URL, letting you rewrite URLs dynamically.

What is rewrite rule in Apache?

RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.

Where is mod_rewrite so?

The mod_rewrite module is enabled by default on CentOS 7. If you find it is not enabled on your server, you can enable it by editing 00-base. conf file located in /etc/httpd/conf. modules.

Can rewriteengine change the URL of a rewriterule?

Assuming this file enables the RewriteEngine, any RewriteRule could change the URL. A drastic enough change (such as one that points Apache to another directory instead of the original directory it was heading toward) will cause Apache to issue a sub-request and proceed to fetch the new file.

What is mod_rewrite in Apache?

The Apache module mod_rewrite is a very powerful and sophisticated module which provides a way to do URL manipulations. With it, you can do nearly all types of URL rewriting that you may need.

How do I add mod_rewrite to my website?

Load that page up in your web browser, and perform a search for “mod_rewrite”. All being well, you’ll find it in the “Apache loaded modules” section of the page. If it isn’t there, you’ll have to contact your hosting company and politely ask them to add it to the Apache configuration.

How to wrap mod_rewrite code in an ifmodule?

This is possible with any module; simply wrap your mod_rewrite code in an <IfModule> block and you’ll be all set: I hope that this tutorial has proven that mod_rewrite isn’t too scary.


3 Answers

You have two options: Either use a RewriteCond to restrict the already matched string:

RewriteCond $1 !=products
RewriteRule ^([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]

Or since Apache 2, you can also use a negated look-ahead assertion:

RewriteRule ^(?!products/)([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]

Besides that, you can also use the QSA flag instead of appending QUERY_STRING manually. And note that your current pattern will also allow something like /foobarsearch/, so it doesn’t have the separating /.

like image 116
Gumbo Avatar answered Oct 07 '22 00:10

Gumbo


Not exactly "Not matching a word" - but you can add a Condition on the rewrite to say "If the URL doesn't match this pattern"

RewriteCond %{REQUEST_URI} !^/products
RewriteRule ^([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]

Which is generally the most common way to do things like this - as conditions can also be chained together.

You can find more information about RewriteCond here :- http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

like image 31
Mez Avatar answered Oct 06 '22 22:10

Mez


If mod_rewrite supports lookaheads, you can use

RewriteRule ^(?!products)([a-z0-9_-]+)?/?search....

According to this question on serverfault.com newer versions do.

The part (?!products) tells the regex engine to look ahead and fail if it finds "products".

like image 21
Jens Avatar answered Oct 06 '22 22:10

Jens