Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite - How to reference to root directory

Rewrite URLs for SEO back to the root directory

         

AngusS

9:08 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



Hi!

I have a problem which I am sure is small, but for me has turned into a very late night of trying to get this working, and I was wondering...hoping...praying someone wouldn't mind giving me some help.

On our company website, I want to have URLs that would be like www.example.com/parts/randompartnumber.html

So, I proceeded to write the rule in .htaccess like this...

RewriteEngine On
RewriteRule ^parts/(.*).html part.php?search=$1

...and then placed a part.php file in the /parts directory and changed the links to have ../ in front.... That works great....
Then I come to our other site which is hosted by 1and1 and tried to do the same, but it didn't work. Found out that I need to put in

RewriteBase /

Ok, so now it works, however, changing the links in the parts.php file to ../ doesn't work....it still tries to reference to the /parts directory. Not wanting to load the whole website into /parts and feeling sure there must be a better way, I read about and tried several different rules including RewriteCond statements, but no real success.

I now have...

RewriteEngine on
RewriteBase /
RewriteRule parts/(.*).html <part.php?search=$1

Which works to redirect a written URL in the form of www.example.com/parts/partnumber.html to the php in the root directory and search fine, but it does not rewrite it...that is, the URL displayed is still written as www.example.com/part.php?search=partnumber

I am sorry, I have a feeling this is super simple, but I am totally missing the solution so far.

Thanks SO much for your help!

Angus

jdMorgan

3:26 am on Apr 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change all links on the "parts" pages to the form "/parts/<whatever>"
Then use:

RewriteEngine On
RewriteRule ^parts/(.*)\.html$ /part.php?search=$1 [L]
RewriteRule ^parts/(.*)$ /$1 [L]

The first rule handles the "pages" in the pseudo-directory "/parts"
The second rule rewrites any references to objects linked-to or included on those pages.

Note that the code (as written) goes into .htaccess in the directory above "/parts" and "part.php" goes in that directory as well. If "part.php" is placed in the "/parts" subdirectory, you'll need to explicitly exclude "parts.php" from being rewritten (to itself) by the second rule. It is already implicitly excluded from the first rule, though, because it doesn't end with ".html".

Jim

AngusS

5:24 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Thanks so much Jim, you are a champion!

That works perfectly. I found I had to change any links on the part.php page to /contact.html instead of just contact.html and it works like gold!

Man, thank you so so much, I really appreciate your help!

Have an awesome day!

Angus

jdMorgan

6:24 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The whole problem may be clearer if you bear in mind that it is the clinet (e.g. browser) that resolves relative URLs. If given a page-relative URL, i.e. "page.ext", then the client removes the page-part of the URL it is currently displaying, adds the new page-part (page.ext), and requests that page. If given a server-relative URL, e.g. "/page.ext", then the client remove the entir local URL-path, adds the new URL-part to the domain name, and requests that. So, the requested URL is the result of where the browser "thinks" it is, plus the relative URL-part you provide in the link. In this case, the browser thinks it is in the subdirectory /parts/ and acts accordingly.

Jim

AngusS

6:33 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Ok, great! Thanks, now that does make sense.

Honestly, thanks so much for your help...I really needed to know that answer quickly and I so appreciate your quick and helpful responses!

Thanks again!

Kind regards,

Angus