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.
The $1 is basically the captured contents of everything from the start and the end of the string. In other words, $1 = (. *) .
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.
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.
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.
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.
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.
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.
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.
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 /
.
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
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With