Forum Moderators: phranque

Message Too Old, No Replies

re-write problem

         

Mohamed

4:14 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



hi all
I would like to convert this url http://example.com/project/foo/New-Application to http://example.com/project/foo.php?name=New-Application

here is my code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^foo/$ /foo.php [L]
RewriteRule ^foo/([A-Za-z]*) /foo.php?name=$1 [L]

I am getting 404 document not found whenever I try to visit http://example.com/project/foo/New-Application
so what am I doing wrong. please help.

jdMorgan

4:39 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your character-match group does not include the hyphen character. Therefore, URLs containing hyphens will not match the rule as intended.

RewriteConds apply only to the *single* RewriteRule that follows them.

Use the [NC] flag to make pattern matches case-insensitive, rather than using [A-Za-z].

Using "+" instead of "*" in the second rule will make the code work with the rules in either order.

I'd suggest:


RewriteEngine on
RewriteRule ^foo/$ /foo.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foo/([A-Z\-]+)$ /foo.php?name=$1 [NC,L]

Whenever you get an error, check the server error log; The information it provides is often quite helpful.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

Mohamed

6:01 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



Thanks.
It works.

Mohamed

6:40 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



I have tried everything but I could not create decent url rewriting code in apache.
therefore I designed my urls like this way
http://www.example.com/lyrics/artist.php/J-LO/Get-right/
the artist.php takes the two parameters behind it.
so what do you think about it pros and cons.

Thanks in advance.

jdMorgan

7:00 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't suggest anything without knowing all about your site.

But if "lyrics" in the URL means that you *always* call artist.php, then you could use URLs like

http://www.example.com/lyrics/J-LO/Get-right/

and use a rule like this example:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lyrics/([A-Z\-]+)/([A-Z\-]+)/?$ /artist.php?artist=$1&title=$2 [NC,L]

By the way, the RewriteConds require a call to the file system to see if the requested URL exists as a file or a directory. This is a relatively slow function. If all URLs in the virtual /lyrics/ directory and its subdirectories always call artist.php (or some other .php file) in your root directory, then you don't need those two RewriteConds, and you should get rid of them.

Think about how the virtual directories in requested URLs are associated with real files or with scripts. This will help you write rules to map URLs to files and scripts.

Jim

Mohamed

7:19 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



Thanks for your help.

my site is portal which has two sections. lyrics section and music section.
in my lyrics section I have three main files which are lyrics/artist.php, lyrics/song.php and lyrics/atoza.php and same applies to music section.
my current urls look like this
http://www.example.com/lyrics/artist.php?artistname=J-LO
http://www.example.com/lyrics/song.php?artist=J-LO&song=Get-right
http://www.example.com/lyrics/atoza.php?letter=j
so what I would like to have is
http://www.example.com/lyrics/J-LO/
http://www.example.com/lyrics/J-LO/Get-right/
http://www.example.com/lyrics/j/

I have attempted your example in the library but with no luck.

jd01

7:49 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're going the wrong way.

Have a look at the tutorials in the library to get an understanding of what you need to do and you should have a good idea of where you are going wrong.

Check the 'Beginngers' and 'Changing Dynamic URLs to Static' entries.
(I think they have the exact explanation you need.)

Apache Tutorials and Library [webmasterworld.com]

Justin