RewriteRule

Hi! could you say me why this rule doesn’t work?

RewriteRule ^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([a-zA-Z0-9_-]+);$ index.php?SID=$1&id=$2&$3=$4 [L]

Welcome to the SP forums.

Could you explain what you want it to do, and what it is doing instead?

I want to change url into index;variable of sid;variable of id;variable of p or x;
but when I use this

RewriteRule ^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([a-zA-Z0-9_-]+);$ index.php?SID=$1&id=$2&$3=$4 [L]

rule I get 404error:confused:

Could you show a few example URLs ? I’m not entirely sure what you’re getting at.

[noparse]http://localhost/gb/dk/trr/index;jba038gqcfubuu2mueskkvt8f4;main;2;[/noparse] for example.

RewriteRule ^index;([a-zA-Z0-9_-]+);$ index.php?SID=$1 [L]

when i use this one it works perfect and I get url like this:
[noparse]http://localhost/gb/dk/trr/index;jba038gqcfubuu2mueskkvt8f4;[/noparse]

The rule doesn’t match

This is your expression


^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([a-zA-Z0-9_-]+);$ 

And this is the URL

index;jba038gqcfubuu2mueskkvt8f4;main;2;

The first part, index; matches, and then ([a-zA-Z0-9_-]+); matches jba038gqcfubuu2mueskkvt8f4;. Next ([a-zA-Z0-9_-]+); matches main; but then there is a problem, since (p|x) must match a p or an x, but neither is there, there is a 2, and a 2 neither a p nor an x. Then there is more stuff in the rule, but the URL stops, so that also won’t work.

I’d go back to see what kind of URLs you’d actually like to accept and then tailor the rule specific to that.

If you want to make stuff optional, you can use a question mark (?)

For example


^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([a-zA-Z0-9_-]+)[COLOR="Red"]?[/COLOR];[COLOR="Red"]?[/COLOR]$ 

would make the last bit optional.
Alternatively you could make several rules, going from short to long, like


RewriteRule ^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x); index.php?etc$ 
RewriteRule ^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([a-zA-Z0-9_-]+); index.php?etc$ 

HTH :slight_smile:

No no no you think that 2 is p or x , if yes its wrong, because 2 is variable of p or x look:index.php?SID=" . session_id() .
"&id=main&p=2

index.php?SID=" . session_id() .
"&id=reg&x=2

and p or x is $3 in rule and variable is $4

RewriteRule ^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([a-zA-Z0-9_-]+);$ index.php?SID=$1&id=$2&$3=$4 [L]

I’m so confused I never had so stupid problem :injured: maybe you could just write me a rule which would work for both URLs:
index.php?SID=" . session_id() .
"&id=main&p=2

index.php?SID=" . session_id() .
"&id=reg&x=2
please:)

In order to get the p or x, it has to be in the URL. We can’t just expect Apache to guess, now can we? :slight_smile:

So your URL would become something like

index;jba038gqcfubuu2mueskkvt8f4;main;p;2 or index;jba038gqcfubuu2mueskkvt8f4;main;x;5, etc

I normally don’t just give away rules (but rather let the one who asks figure it out) but since you were already so close … Here ya go:


RewriteRule ^index;([a-zA-Z0-9_-]+);([a-zA-Z0-9_-]+);(p|x);([0-9]+)$ index.php?SID=$1&id=$2&$3=$4 [L]

But do take care that the “p” or “x” is in the URL. It has to be, otherwise it doesn’t work!