Forum Moderators: phranque

Message Too Old, No Replies

simple redirect in htaccess not working as expected

         

alphabono

9:15 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



I'm trying to do a simple rewrite where:
www.example.com/portfolio/people

would redirect to:
www.example.com/gallery/people/template.php?page=index

The rewrite rule I thought would work, but doesn't, is shown below:
RewriteRule ^portfolio/people$ gallery/people/template.php?page=index [L]

The .htaccess file is located in the root directory (www.example.com/)

If I change the rule to a redirect with [R] like this:
RewriteRule ^portfolio/people$ gallery/people/template.php?page=index [R]
it successfully redirects the page and loads from www.example.com/portfolio/people as it should, but of course displaying what I was trying to rewrite in the first place.

Any help would be most appriated!

RedAndy

9:52 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Hi alphabono,

If this is just a one page redirect then you could use a RedirectPermanent instead:

RedirectPermanent /portfolio/people [your-site-name.com...]

Or are you redirecting a lot of pages such as /portfolio/people/dave to /gallery/people/template.php?page=dave as well?

Andy

alphabono

11:38 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



I would be redirecting many pages.

Strangely enough, if I redirect www.example.com/portfolio/people to:
www.example.com/gallery/people/template.php?page=test

Using the same rewrite rule that wasn't working with page=index using page=test:
RewriteRule ^portfolio/people$ gallery/people/template.php?page=test [L]

It is rewritten correctly.

I guess the problem lies in the nature of index file? It seems as though the relative paths are getting messed up when I try to use the rewrite rule with the index file.

Is there something different with respect to current working directory path when using mod_rewrite vs when entering in the actual URL?

i.e. if I enter:
www.example.com/portfolio/people and have the rewrite rule:
RewriteRule ^portfolio/people$ gallery/people/template.php?page=index [L]
in my .htaccess file in the root directory, do I end up with a different working directory than when I enter:
www.example.com/gallery/people/template.php?page=index
?

It seems as though this is where the root of my problem lies as the index.htm file has some relative links within it which aren't translated as I would expect.

Any suggestions?

Thanks,

Ranken

jdMorgan

7:08 pm on Mar 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember that is it the client (browser) that resolves relative links. They are resolved by adding the relative link on the page to the browser's current 'directory' location. Therefore, if you 'move things' using rewrites on the server, you can confuse the browser.

The usual solution is to use canonical URLs or to use server-root-relative URLs for images and other included objects such as stylesheets and external JS files, e.g. <img scr="http://example.com/images/my_logo.gif"> or <img src="/images/my_logo.gif">

Jim