Forum Moderators: phranque

Message Too Old, No Replies

Forward Slashes in mod rewrite Cause Page to Display Odd-Like

forward slashes in mod_rewrite

         

hizzle

3:11 am on Apr 4, 2008 (gmt 0)

10+ Year Member



Hello there. I've looked for the answer to this problem and since I couldn't find it, I decided to do something I rarely do and post for help.

My problem is that when I use a forward slash after "^article" in the below set of rules, I do not receive an error page when attempting to access the URL, but I get, for a lack of a better term, a funky looking page with the correct data on what seems to be the right page. No images display, links look generic, banner on the right is actually displayed on left, etc. I know it's the forward slash after "^article" doing this because anything else after "^article" will work just fine.

For example, the following works properly when using an underscore after "^article".

Options +FollowSymLinks
RewriteEngine on
ReWriteBase /
RewriteRule ^article_([^./]+)$ /details.php?articleID=$1/ [L]

However, the following causes the problem stated above when using a forward slash after "^article"

Options +FollowSymLinks
RewriteEngine on
ReWriteBase /
RewriteRule ^article/([^./]+)$ /details.php?articleID=$1/ [L]

Is there something I'm doing wrong? Could/Should I try something else? Any help would be greatly appreciated. Thanks.

jdMorgan

4:36 am on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember that it is the browser which resolves page-relative URLs in links and included object references on your page, such as <img src="images/logo.gif">. So, by adding a slash to the URL, the browser will be trying to resolve the relative link in relation to an additional subdirectory level. This is likely breaking your image includes, CSS includes, and on-page links.

The two ways to fix it while still using slashes in your article-name URLs are to either use server-relative or canonical links, or to detect the requests that contain additional subdirectory levels, and rewrite them to correct them. If you have a choice, don't use slashes. If not, then use
<img src="/images/logo.gif"> or <img src="http://www.example.com/images/logo.gif"> instead of <img src="images/logo.gif">

Jim

hizzle

11:25 am on Apr 4, 2008 (gmt 0)

10+ Year Member



Jim,

Thanks for that explanation. I now understand what's going on. Since it's not detrimental for me to use slashes and rewriting very many links seems unnecessary at this point, I will opt not to use forward slashes. Again, thanks.