Forum Moderators: phranque

Message Too Old, No Replies

yet and still mod_rewrite help

         

Dapuzz

11:27 am on Jun 11, 2005 (gmt 0)

10+ Year Member



Hi all
I need 2 levels of rewritting:
from /foo or /foo/index.php or /foo/
to index.php?cat=foo

from /foo/bar or /foo/bar/index.php or /foo/bar/
to index.php?cat=foo&id=bar

Any suggestion?
Thanks.

moltar

11:44 am on Jun 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this. I did not test.

RewriteRule ^([^/]+)/?(\d+)?/?$ /index.php?cat=$1&id=$2

Dapuzz

12:26 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



internal server error 500 :(
I think due to infinite redirection loop

jdMorgan

2:56 pm on Jun 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you're rewriting to the same effective URL, you'll need to use RewriteCond to test the query string and only rewrite if it is blank. Otherwise, the code will loop.

Jim

Dapuzz

3:25 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



Now i'm going to search something in the official guide.
Can you write a small example for me when you will have some time to wast please?

blitzer

5:08 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



Perhaps you have the same problem as I...
when I try this
RewriteRule îndex.\html$ index.html?lang=en do not work! He ignores all behind 'index.html', (error 500, loop). I don't know why it is and I'm looking for a solution. If i use
RewriteRule îndex.\html$ index2.html?lang=en, only index2.html will be loaded, not index2.html?lang=en.
I'm using Version 1.3

Dapuzz

5:44 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



no, perhaps your syntax is not correct.
The point must be escaped "\." not ".\"

I'm waiting for the example thath jdMorgan promise me. Np, I'm not in hurry...

blitzer

8:25 am on Jun 12, 2005 (gmt 0)

10+ Year Member



Only a mistake in writing ... I mean
RewriteRule îndex\.html$ index.html?lang=en do not work! He ignores all behind 'index.html', (error 500, loop). I don't know why it is and I'm looking for a solution. If i use
RewriteRule îndex\.html$ index2.html?lang=en, only index2.html will be loaded, not index2.html?lang=en.

jd01

9:11 am on Jun 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi -

A RewriteRule only 'sees' information before a query string (stuff after the ?). To test for a query string, you have to use a condition. The looping problem stems from this, because the RewriteRule never sees that there is the necessary information after the ?.

To break the looping, you will need to test to see if there is a query string exist and if so, exit:

RewriteCond %{QUERY_STRING} !^.
RewriteRule ^index\.html$ index.html?lang=en [L]

OR, for this:
from /foo or /foo/index.php or /foo/
to index.php?cat=foo

In the 'root' .htaccess file:
RewriteCond %{QUERY_STRING} !^.
RewriteRule ^foo(/(index\.php)?)?$ /index.php?cat=foo [L]

The Condition:
! = is not
^ = begin the line
. = any character, that is not the end of a line.
($) left off to use the implicit 'and anything else'

The Left Side of the Rule:
^foo(/(index\.php)?)?$

^ = begins the line
(/ = begin 1st grouping to make the / optional
(index\.php) = full 2nd grouping to make index\.php optional
? = 0 or one of the preceding characters or groups
)? = close the 1st group and make both groups optional
$ = ends the line

The Right Side of the Rule:
/ = A rewrite issues a new request to the server, so a preceding / is required
index.php?cat=foo = where we want to go
[L] = if this rule qualifies, stop processing now (should always be included, unless you are absolutely certain you should not have it.)

At times it is necessary to use the QSA (Query String Append) 'flag' or 'directive', which lets Apache 'know' it should append, or re-append more information to the rewrite. This would change [L] to [QSA,L]

Hope this helps.

Justin

Dapuzz

10:19 am on Jun 12, 2005 (gmt 0)

10+ Year Member



oh oh you are a man! This works great for me! Thanks a lot!

#1)
RewriteCond %{QUERY_STRING}!^.
RewriteRule ^([^/]+)(/(index\.php)?)?$ /index.php?cat=$1 [L]
#2)
RewriteCond %{QUERY_STRING}!^.
RewriteRule ^([^/]+)/([^/]+)(/(index\.php)?)?$ /index.php?cat=$1&id=$2 [L]
:kiss::kiss::kiss:

blitzer

10:46 am on Jun 12, 2005 (gmt 0)

10+ Year Member



Hi!

I've tried:
RewriteCond %{QUERY_STRING}!^.
RewriteRule ^index\.html$ index.html?lang=en [L]

Now, there is no loop, but only the 'normal' index.html will be loaded.
With QSA-flag, the same problem...

blitzer

10:59 am on Jun 12, 2005 (gmt 0)

10+ Year Member



Why this solution doing what I want?

RewriteCond %{QUERY_STRING}!^.
RewriteRule ^index\.html$ [fullpath.com...] [QSA,R]

Dapuzz

2:10 pm on Jun 12, 2005 (gmt 0)

10+ Year Member




I've tried:
RewriteCond %{QUERY_STRING}!^.
RewriteRule ^index\.html$ index.html?lang=en [L]

in the right part of the rule ht page must start whith a "/"
RewriteCond %{QUERY_STRING}!^.
RewriteRule ^index\.html$ /index.html?lang=en [L]