Page 1 of 1
RegExp and Apache RewriteEngine
Posted: Mon Nov 21, 2005 6:37 am
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
Posted: Mon Nov 21, 2005 8:08 am
by yum-jelly
Why are you escaping the * \? * ?.
yj
Posted: Mon Nov 21, 2005 8:14 am
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.
Posted: Mon Nov 21, 2005 9:29 am
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
Posted: Mon Nov 21, 2005 9:38 am
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.
Change %N to %1 and that should help ... hopefully.
Posted: Mon Nov 21, 2005 11:14 am
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.
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
Posted: Mon Nov 21, 2005 12:22 pm
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?
Posted: Mon Nov 21, 2005 3:29 pm
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
Posted: Mon Nov 21, 2005 3:40 pm
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).
Posted: Thu Nov 24, 2005 2:26 am
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]