Forum Moderators: phranque

Message Too Old, No Replies

Mod Re-Write (E.g index.php?list=a)

Help with mod re-write please!

         

PumpkinHead

10:52 am on Sep 29, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm experimenting with mod rewrite at the moment and I'm a little stuck.

I have the following links that will exist in the header of every page on my site:

<td background="images/img_button.gif" class="sty_button">
<a href="a/">A</a>
</td>

<td background="images/img_button.gif" class="sty_button">
<a href="b/">B</a>
</td>

....

<td background="images/img_button.gif" class="sty_button">
<a href="z/">Z</a>
</td>

Which I want to direct to the following url(s):

index.php?list=a
index.php?list=b
....
index.php?list=z

My .htaccess code is as follows:

---------------------------------------
RewriteEngine on
RewriteRule ^index.html$ index.php
RewriteRule ^a/?$?list=a
RewriteRule ^b/?$?list=b
....
RewriteRule ^z/?$?list=z
---------------------------------------

I now have the following problems...

a) Once I click a link, I am directed to the new url correctly, but the background image is not shown anymore.
b) If I've clicked the link, and I'm now at mydomain.com/a/ I click the link again and it takes me to mydomain.com/a/a/ and so on.

What am I doing wrong?!

jd01

11:10 am on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is the links, not the mod_rewrite code:

a href="a/" is directory relative -- it will always request the location from where your are in the directory structure now.

a href="/a/" is server relative -- it will always request the location from the server root.

The images are the same way.

Your server is looking for the images in a/images/ and then in a/a/images/.

Your code could be a little more sound and compact like this:

RewriteEngine on
RewriteRule ^index\.html$ /index.php [L]
RewriteRule ^([a-z]{1})/?$ /index.php?list=$1 [L]

1. Escaped the .(dot) -- meta character for 'anything except the ending of a line'
2. Change the 1st rule to a server relative path.
3. Changed the 2nd rule to any letter from a to z, added a server relative path, and back-reference ($1) to the rewrite path.
4. Added a last [L] flag to each rule.

Hope this helps.

Justin

PumpkinHead

11:29 am on Sep 29, 2005 (gmt 0)

10+ Year Member



Thanks for the help, however is there a way that I can specify the root in .htaccess?

Once I upload things, everything will work fine but as I am testing on my localhost the root set to [localhost...] which gives me an error as the files (index.php) are found in [localhost...]

jd01

11:36 am on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could probably remove the preceding / and add a couple of rules while you are testing to redirect any deeper requests back to the original:

RewriteEngine on
RewriteRule ^index\.html$ index.php [L]
RewriteRule ^([a-z]{1})/?$ index.php?list=$1 [L]

# For Testing
RewriteRule ^[a-z]{1}/([a-z]{1})/?$ $1/ [R,L]
RewriteRule ^[a-z]{1}/images/(.+)$ images/$1 [L]

Something like the above should get you close...

Justin

PumpkinHead

1:37 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



No luck :(

When I use that code no images are displayed and the link s still go back to localhost root?

I've tried using the following but this also didn't work...

DocumentRoot "C:/localhost/myfiles/examples/modrewrite/"

?