Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite /dir1/file1.html -> /index.php?=file1

mod_rewrite /dir1/file1.html -> /index.php?=file1

         

bloggy

1:14 pm on Sep 21, 2007 (gmt 0)

10+ Year Member



Hi all,

hope fully this is the right forum for my question. I want to have a structure like this on my site:

www.mydomain.com/dir1/file1.html
www.mydomain.com/dir2/file1.html
etc.

but dir1, dir2 etc. do NOT exist. I want to make use of mod_rewrite and do this:

www.mydomain.com/dir1/file1.html ==> www.mydomain.com/index.php?id=file1

Can andybody help me or tell me how the code for this must look like?

Thank you so much!

g1smd

1:19 pm on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have a look through the last few days posts here.

This question has been asked and answered at least several times in the last few days.

bloggy

9:42 pm on Oct 2, 2007 (gmt 0)

10+ Year Member



Hi,

ok I researched for a while now but I did not came up with a solution, yet. Please could anybody give me an example on how to do this?!

So far I have:

RewriteRule ^myfolder/([a-z0-9_-]*)\.html$ /index.php?_id=$1 [NC,L]

The problem with this is that all links, images etc. are incorrect then. For example:

mydomain.com/myfolder/index.php

instead of

mydomain.com/index.php

Any suggestions?

Of course I could edit all Urls of the links and images and make them absolute (e.g. www.mydomain.com/index.php) but doesn't that look "bad" for Google Spider? Also, I would have to change hundreds of links ...

g1smd

10:53 pm on Oct 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you only want the redirect to apply to .html URLs?

Defining exactly how it is going to work takes longer than writing the code to actually make it work.

The problem you are seeing is that relative links are resolved by the browser and are treated as being relative to the URL that is shown in the browser URL bar.

Page URLs are defined by what URL is in the link that someone clicks on. If you need a new URL, then that link must be edited. I use links that start with a / and so are fixed relative to the root of the site.

jdMorgan

3:24 am on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course I could edit all Urls of the links and images and make them absolute (e.g. www.mydomain.com/index.php) but doesn't that look "bad" for Google Spider? Also, I would have to change hundreds of links ...

It is not necessary to use an absolute (canonical) URL. All you need to do is to change page-relative links to server-relative links, e.g. <img src="logo.gif"> becomes <img src="/logo.gif">. This specifies that the image is located at /logo.gif, relative to the root of the domain, i.e. at example.com/logo.gif.

Remember, it is the client (e.g. browser) which resolves relative links, and the browser thinks it is accessing a page in /myfolder and will therefore resolve all page-relative links based on the /myfolder location.

The other alternative is to add a rule to internally rewrite requests for all images, CSS, external JS files, etc. from "/myfolder" back to "/".

Jim