L flag does not work like I want

New to mod_rewrite? This is a good place to start.

L flag does not work like I want

Postby dummes_L_flag » Thu May 21, 2009 11:38 am

Hello,

In don't understand how the L flag works. This is my configuration:

# START
RewriteEngine on
Options +FollowSymLinks
RewriteBase /

#some rules
RewriteRule ^tischtennisschlaeger-fertigschlaeger.htm$ tischtennisschlaeger-fertigschlaeger.php [L]

#more rules
RewriteRule ^stiga-tischtennis-belaege.htm$ tischtennis-belaege-htaccess.php?hersteller=stiga&%{QUERY_STRING} [L]

#etc
RewriteRule ^tibhar/tischtennis-belaege-tibhar.htm$ tischtennis-belaege-htaccess-2.php?hersteller=tibhar&%{QUERY_STRING} [L]

#LAST RULE. Here is the problem. Why this rule is alway true?
RewriteRule . 123.php

# END


The LAST RULE is always hitted. Every rule on the top has the L-Flag. But when the last rule in my htaccess is activated, every request goes to 123.php. Why?
dummes_L_flag
 
Posts: 3
Joined: Thu May 21, 2009 11:23 am

Postby richardk » Thu May 21, 2009 4:09 pm

The request to tischtennisschlaeger-fertigschlaeger.php, etc. is a new internal request and so the mod_rewrite is processed again.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(tischtennisschlaeger-fertigschlaeger\.)htm$ /$1php [L]
RewriteRule ^stiga-tischtennis-belaege\.htm$ /tischtennis-belaege-htaccess.php?hersteller=stiga [QSA,L]
RewriteRule ^tibhar/tischtennis-belaege-tibhar\.htm$ /tischtennis-belaege-htaccess-2.php?hersteller=tibhar [QSA,L]

# Don't match internal sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule . /123.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dummes_L_flag » Sat May 23, 2009 9:53 am

Thank You richard,

I've changed Your hint from
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule . /123.php [QSA,L]


to
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*).htm$ 123.php [QSA,L]


and it works so far. Thank You very much.
But I don't get it to work, that only the root directory htm-files are rewritten. Every non-existing .htm-request in sub-directories are rewritten too.
Now this works (great, I'm happy)
Code: Select all
http://www.meine-webseite.de/everything.htm is rewritten to 123.php (cool)


but
Code: Select all
http://www.meine-webseite.de/non-existing-directory/everything.htm is also rewritten to 123.php. (not wanted)
http://www.meine-webseite.de/non-existing-directory/a/b/c/everything.htm is also rewritten to 123.php. (not wanted)


2 hours, and I don't get it.
dummes_L_flag
 
Posts: 3
Joined: Thu May 21, 2009 11:23 am

Postby richardk » Sat May 23, 2009 4:18 pm

But I don't get it to work, that only the root directory htm-files are rewritten. Every non-existing .htm-request in sub-directories are rewritten too.

Code: Select all
^(.*).htm$

The .* means match everything (including non existing directories).

Code: Select all
^[^/]+\.htm$

Will only match .htm requests in the document root because [^/] does not match slashes (/).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dummes_L_flag » Sun May 24, 2009 1:46 am

This works fine, I'm very glad Richard.
Code: Select all
^[^/]+\.htm$


Maybe I should buy a good book abaout this stuff, because I neither understand the modrewrite module, nor the perl search pattern construction. Both - modrewrite and perl patterns - are the devil himself, in my opinion ;-)

Best Regards from germany
Adrian
dummes_L_flag
 
Posts: 3
Joined: Thu May 21, 2009 11:23 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 93 guests

cron