views:

13

answers:

1

On my main page, the navigation links are as follows:

  • localhost/inventory/add
  • localhost/inventory/view

The code for those are:

<a href='add'>Add Record</a>
<a href='view'>View Records</a>

and my .htaccess file is:

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ ./index.php?url=$1 [L]
RewriteRule ^([^/\.]+)/?/([^/\.]+)/?$ ./index.php?url=$1&id=$2 [L]

The problem I am having is that for the records, there is an edit link that takes them to:

  • localhost/inventory/edit/1

For record one, and the code for that link is:

<a href='edit/1'>Edit</a>

Which is fine, except now the navigation links take them to:

  • localhost/inventory/edit/add
  • localhost/inventory/edit/view

Because it thinks it is in an 'edit' directory.

Does anyone know of an idea to overcome this problem?

Thank you!

A: 

This probably not because .htaccess, but the difference between relative versus absolute links. That is to say your navigation links should probably be prefixed with /, like:

<a href='/inventory/add'>Add Record</a>
Jason McCreary
Thank you for your response Jason, putting the / makes the link relative to the server root. I guess I could have a config file and any subdirectory the script is running in would need to be assigned, but I was hoping to avoid that.
Doug Barrett
Sorry. This is web urls 101. If the link is simply `add` it's always going to be relative to the current directory. I don't think you're going to have much luck creating a rewrite rule that gets around that.
Jason McCreary
OK thank you, that wasn't the answer I was hoping for but its the answer I was unfortunately anticipating.
Doug Barrett
Sorry. Again, not impossible, but don't think you'd have much luck. That's why most frameworks include rewrite rules and their own link generation functions. Instead of just rewrite rules.
Jason McCreary