0

This might be a stupid question but it took me days to even figure out what the problem was.

I'm using mod_rewrite and passing values separated by forward slashes.

The problem is that the forward slashes are changing the path (I think)

So I'm passing "admin/login" which gets directed to admin.php with the action parameter as login.

It works fine if i use %2F to encode the forward slash but then my url looks like www.mysite/admin%2Flogin which isn't as nice as www.mysite/admin/login.

It was the css that made me realise what was happening. The login page loaded with no style and a link back to the homepage came up as www.mysite/admin instead of www.mysite.

Can anyone help me out here. I'm a bit stuck. Is there a way to get apache to stop seeing forward slashes as a path change without encoding them?

Cheers.

Just wanted to add that i've now discovered the first rule in my comment below works fine if i change the forward slash to an equals sign. It stops adding to the website path. Could it be that the behavior i'm describing is actually normal? Confused...

------- Edit 1 ------------ Here's the rules. Anything with a slash causes the behavior I mentioned.

RewriteRule ^(admin)/(login)$ admin.php?action=login [L]

RewriteRule ^(admin)$ admin.php?action=all-articles [L]

RewriteRule ^(admin)/([a-zA-Z0-9_]+)$ admin.php?action=articlesByTypeTag&topcat=$1 [L] 

------- Edit 2 -------------- Ok so after loads more faffing about, and for those who are as confused as i am (if there are any of those) here's the latest.

So the forward slash definitely adds to the Document root as far as the browser is concerned. Here's a basic example. On the login page, which the first rule in the list above handles, i have a link back to the homepage. It was simply href=".", which worked fine before i started using mod_rewrite.

Now the "admin" in "admin/login" is added to the path so that when i hover over the homepage link it displays "localhost/admin/". I changed it to href="/home" and now it works fine.

I added a forward slash to the links for my css files and now they work fine too. I'm assuming that this is because the paths are now relative to the document root in the virtual host rather than relative to what the browser thinks.

Maybe the problem here is that the whole path thing is more complicated than i thought it was. What i wanted was for mod_rewrite to get the values from "admin/login" and apply them to the correct .php with the correct parameter, which is what happens. It's just that the change in how the browser sees things is confusing.

Can anyone tell me if this is how it should be, or is there something really wrong with my setup?

Jenny D
  • 27,780
  • 21
  • 75
  • 114

1 Answers1

0

The login page loaded with no style and a link back to the homepage came up as www.mysite/admin instead of www.mysite.

Because you were using relative URLs in your client-side HTML. This doesn't really have anything to do with mod_rewrite or server-side "setup". Yes, "this is how it should be".

The browser simply resolves relative URLs relative to the current URL (the URL you see in the browser's address bar). (However, this can be overridden if you have a base HTML element - see below.) The browser does not know anything about the server config. So, if you have an HTML page located at the URL http://example.com/url/path/to/foo and you have a relative link (CSS, JS, image or anchor) of the form href="subdir/bar", then the browser will naturally resolve this to http://example.com/url/path/to/subdir/bar, and this is the URL that is requested. This is regardless of what file on the server actually handles the request (in this case it might be something completely different like /home/www/user/public_html/cgi-bin/script). URLs and (server-side) filesystem paths are two different things.

To solve this problem you need to either use root-relative URLs (starting with a slash) in your client-side HTML (as you have done), or even absolute URLs (scheme + hostname).

As mentioned above, an alternative (workaround) to "fixing" your relative URLs is to use the base element in the head section of your HTML page. This overrides the current URL for relative links and states the URL that all relative links are relative to. For example, to make all relative links relative to the root document, then you could do something like the following:

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

However, using the base element is not without its caveats. In-page links using a fragment identifier only (eg. #top) are now relative to the URL specified in the base element, which is rarely desirable.

MrWhite
  • 12,647
  • 4
  • 29
  • 41