Forum Moderators: phranque
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?!
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
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