Forum Moderators: phranque

Message Too Old, No Replies

Simple mod rewrite Problem

A simple mod_rewrite question from a newbie

         

fishcake91

3:53 am on Aug 23, 2008 (gmt 0)

10+ Year Member



I have a website that is using dynamic inclusion like this:

[somesite.com...]

Using mod_rewrite, I manage to rewrite the URL above into:

[somesite.com...]

The mod_rewrite rule is as follows:

RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

It works fine when there is no forward slash at the back of the URL. But when I place a forward slash at the end like this:

[somesite.com...]

The page loads, but without stylesheet (CSS) applied to it. I checked the page source of both of them and they are the same.

jdMorgan

4:13 am on Aug 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By adding the slash, you have told the browser that the page URL refers to a subdirectory named /home/. Therefore, the browser will resolve all page-relative links to CSS, images, and other included objects relative to that directory level, rather than to the root directory level. The solutions are to change all of your page-relative links to server-relative (starts with a slash - /image.gif) or canonical (http://www.example.com/image.gif) links, or to add exceptions to the rewrite rule and handle the URLs for these objects separately.

You do not need to escape the "." within a grouped alternate set - "[^/.]" should work fine.

Jim

jdMorgan

4:16 am on Aug 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, be aware that by making the trailing slash optional, you are creating duplicate content. A better solution would be to pick one convention or the other, and then 301-redirect the non-preferred type of URL to the preferred type. If you are referring to "pages," then by convention, their URLs should not have a trailing slash, since a trailing slash implies a directory, not a page.

Jim

fishcake91

3:00 am on Aug 24, 2008 (gmt 0)

10+ Year Member



Thank you! That solved my problem! I knew it has something to do with directories, but I never thought about doing that. Stupid me :P