RegExp and Apache RewriteEngine

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

RegExp and Apache RewriteEngine

Post by visionmaster »

Hello,

I'm not sure wether this is a question for the Apache group or RegExp group, my feeling says more a RegExp thing.

Entering
http://www.somewebpage.net/dev/cgi-bin/ ... tion=suche
into the webbrowser should redirect to another page.

But following Error message is outputed:

"Not Found

The requested URL /dev/cgi-bin/suche.cgi was not found on this server."

.htaccess:
------------
RewriteEngine On
RewriteRule ^.*dev/cgi-bin/suche\.cgi\?pos=10&engine=b2b&suchwort=(.+)&action=suche$ http://www.itsbetter.de/suchmaschine/$1/$1.html

--------------

Entering
http://www.somewebpage.net/dev/cgi-bin/ ... tion=suche
into the webbrowser should redirect to another page.

That works fine, notice instead the question mark ? between suche.cgi and pos, I inserted an underscore _
Strangely this works fine, but I really don't know what I'm doing wrong in the above RegExp.


.htaccess:
------------
RewriteEngine On
RewriteRule ^.*dev/cgi-bin/suche\.cgi_pos=10&engine=b2b&suchwort=(.+)&action=suche$ http://www.itsbetter.de/suchmaschine/$1/$1.html


Thanks for your help,
visionmaster
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

Why are you escaping the * \? * ?.

yj
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Code: Select all

RewriteRule ^.*dev/cgi-bin/suche\.cgi\?pos=10&engine=b2b&suchwort=(.+)&action=suche$
That doesn't work because the query string is passed seperately from the URL. You need to use RewriteCond and the QUERY_STRING variable. Use %N to back-reference the last matched RewriteCond pattern.
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

Hi,

Thanks for your quick reply!
Buddha443556 wrote:

Code: Select all

RewriteRule ^.*dev/cgi-bin/suche\.cgi\?pos=10&engine=b2b&suchwort=(.+)&action=suche$
That doesn't work because the query string is passed seperately from the URL. You need to use RewriteCond and the QUERY_STRING variable. Use %N to back-reference the last matched RewriteCond pattern.
Hmm, I really don't understand your point. I read through some manuals and threads, without success.

Now I'm getting an Internal Server Error with following Rules:

.htaccess:
-------------
RewriteEngine On
RewriteCond %{QUERY_STRING} ^pos=10&engine=b2b&suchwort=(.*)&action=suche$
RewriteRule ^dev/cgi-bin/suche.cgi$ http://www.webpage.de/suchmaschine/%N/%N.html


http://www.somewebpage.net/dev/cgi-bin/ ... tion=suche should redirect to
http://www.somewebpage.net/suchmaschine ... esign.html

whereas the string webdesign is something dynamic and always varies.

Appreciate your help!
visionmaster
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

In %N the N is a number as in %1, %2, %3 .... just like $1, $2, $3 etc for RewriteRule back refenerances. Sorry if that wasn't clear. It was a little early for me. :oops:

Change %N to %1 and that should help ... hopefully.
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

Buddha443556 wrote:In %N the N is a number as in %1, %2, %3 .... just like $1, $2, $3 etc for RewriteRule back refenerances. Sorry if that wasn't clear. It was a little early for me. :oops:

Change %N to %1 and that should help ... hopefully.
No problem, that was the mistake. A big thank you!!! This is how it works:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^pos=10&engine=b2b&suchwort=(.*)&action=suche$
RewriteRule ^dev/cgi-bin/suche.cgi$ http://www.webpage.de/suchmaschine/%1/%1.html

Entering http://www.webpage.net/dev/cgi-bin/such ... tion=suche

redirects to

http://www.webpage.de/suchmaschine/webd ... tion=suche

The correct result is displayed, but why is the QUERY_STRING itself being concatinated to the end of the url?

Regards,
visionmaster
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

visionmaster wrote:The correct result is displayed, but why is the QUERY_STRING itself being concatinated to the end of the url?
Just add a ? to the end URL like so:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^pos=10&engine=b2b&suchwort=(.*)&action=suche$
RewriteRule ^dev/cgi-bin/suche.cgi$ http://www.webpage.de/suchmaschine/%1/%1.html?
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

Buddha443556 wrote:
visionmaster wrote:The correct result is displayed, but why is the QUERY_STRING itself being concatinated to the end of the url?
Just add a ? to the end URL like so:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^pos=10&engine=b2b&suchwort=(.*)&action=suche$
RewriteRule ^dev/cgi-bin/suche.cgi$ http://www.webpage.de/suchmaschine/%1/%1.html?
That works perfect! But what exactly does the ? do in the above case. I would like to understand what ist happening.

Thanks
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

That works perfect! But what exactly does the ? do in the above case. I would like to understand what ist happening.
It just tells mod_rewrite that you're taking care of the query string and not to append it. The Manual says it better:
Query String

The Pattern will not match against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string containing a query string part. Just use a question mark inside the substitution string to indicate that the following stuff should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just the question mark. To combine a new query string with an old one, use the [QSA] flag (see below).
Substitution of Absolute URLs

There is a special feature: When you prefix a substitution field with http://thishost[:thisport] then mod_rewrite automatically strips it out. This auto-reduction on implicit external redirect URLs is a useful and important feature when used in combination with a mapping-function which generates the hostname part. Have a look at the first example in the example section below to understand this.

Remember: An unconditional external redirect to your own server will not work with the prefix http://thishost because of this feature. To achieve such a self-redirect, you have to use the R-flag (see below).
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

Buddha443556 wrote:
That works perfect! But what exactly does the ? do in the above case. I would like to understand what ist happening.
It just tells mod_rewrite that you're taking care of the query string and not to append it. The Manual says it better:
Thanks for the explanation and quote.

Unfortunately I now have a further question. Following rule works fine redirecting

RewriteEngine On
RewriteRule ^.*/(.+)\.html$ /modules/mod_search.php?action=startSearch&suche[searchstring]=$1&suche[results]=10&suche[bundesland]=18&modus=all


htttp://www.webpage.de/machine/machine.html
redirects to
http://www.webpage.de/modules/mod_searc ... &modus=all


What if I want to exclude specific folders from this rule. A further .htaccess file in the folder? Or add a further rule to the existing .htaccess file?

E.g. http://www.webpage.de/presentation/index.html should really open up index.html which itself consists of frames pointing to further .html files.

Thanks again for your help.[/i]
Post Reply