# [RESOLVED] url rewriting and regular expression

## elno

Hello

I'm hosting a website which use url such as this

```

http://example.com/index.php?eur=accueil&text=sidebar

```

I would like to rewrite this url like this:

```

http://example.com/index-accueil-side.html

```

My trouble is that I'm not very familliarized with regular expression used for setting my RewriteRule.

First I have make a test looking if my Rewrite engine is working well,

Added this in my vhost.conf:

```

<Directory /var/www/localhost/htdocs>

AllowOverride All

RewriteEngine On

RewriteBase / 

RewriteRule   ^test.html$   phpinfo.php  [L]

</Directory>

```

Test is successfull so I think there is no problem with such as simple rule and RewriteEngine is working well.

Next step is finding a rule which will match my url scheme.

I've try this rule thinking that my variable are always a set of letter or a name and [a-z] was the regular expression for all kind of letter which may be found in my variable:

```

RewriteRule ^index-([a-z]*)-([a-z]*)\.html$  index.php?eur=$1&text=$2  [L]

```

This does not work. 

As I have can read that . was the regular expression for all kind all characters I have try this:

```

RewriteRule ^index-(.*)-(.*).html$ index.php?eur=$1&text=$2 [L]

```

And also this does not work.

My url does not change, did anyone has an idea about what I'm doing wrong in my RewriteRule?

ThanksLast edited by elno on Sun May 27, 2007 4:26 pm; edited 1 time in total

----------

## ce110ut

your regular expression (by looking at it) seems correct.  I believe you have it in the wrong order though.

try changing from

```

RewriteRule ^index-([a-z]*)-([a-z]*)\.html$  index.php?eur=$1&text=$2  [L] 

```

to

```

RewriteRule ^index.php?eur=([a-zA-Z].+?)-([a-zA-Z].+?)$  index-$1-$2\.html [L] 

```

----------

## elno

Yes you have right that what I 'have just thinking about, by testing accessing to my web pages using the new scheme:

```

http://example.com/index-accueil-sidebar.html 

```

I have seen that I was accessing to my page like with my ancien scheme so the RewriteEngine do is job.

So with my RewriteRule I have to change all url from:

```

http://example.com/index.php?eur=accueil&text=sidebar 

```

to

```

http://example.com/index-accueil-sidebar.html 

```

and then the RewriteEngine will change url to original one telling the server displaying the right page. 

Is that right?

My goal is here but without having to change all urls in the html code.

The RewriteRule you give me seem logical to do the job but is does not work.

If I understand it, now when the server will get this request (write in html code):

```

http://example.com/index.php?eur=accueil&text=sidebar 

```

He will try to load the page call http://example.com/index-accueil-sidebar.html ?

I feel a little bit confused with this...

----------

## ce110ut

 *elno wrote:*   

> Yes you have right that what I 'have just thinking about, by testing accessing to my web pages using the new 
> 
> The RewriteRule you give me seem logical to do the job but is does not work.
> 
> If I understand it, now when the server will get this request (write in html code):
> ...

 

Yes.  Is that not what you want to happen?

----------

## elno

In your rule

```

RewriteRule ^index.php?eur=([a-zA-Z].+?)-([a-zA-Z].+?)$  index-$1-$2\.html [L] 

```

the first part of expression is what we call on urls and second part is the file that the server will give when he receive the first expression

So the file called

```

http://example.com/index-accueil-sidebar.html

```

have to be present somewhere on my webroot path, is that right? If it is the case that not really what I want. 

and what happen to index.php? there is something I don't understand here in RewriteRule process.

But in fact after thinking about my problem I finally realized that it wasn't a problem rewriting urls in my code because syntax stay quite the same than php code syntax.  

So I have decided to use this rule:

```

RewriteBase /

RewriteRule ^([a-z]*)/([a-z]*)\.html$  index.php?eur=$1&text=$2  [L]

```

and my url on html code is write as this:

```

<head>

<base href="http://www.example.com/" >

</head>

...

<a href="accueil/sidebar.html">index</a>

...

```

Result in browser is now:

```

http://www.example.com/accueil/sidebar.html

```

which is understood by my server as:

```

http://example.com/index.php?eur=accueil&text=sidebar 

```

This do what I was looking for.

If  there is something I have don't understand I will be happy to learn about it but don't waste your time with me as my problem is for now solved.

Thanks a lot for your help and your time!

----------

## ce110ut

ahh I see.  You are right.  Well good!  If you wouldn't mind, could you edit the subject and add "[RESOLVED] "?

----------

