PDA

View Full Version : Simple URLs (but no file or folder at the address)


Seymour Clufley
12-08-2008, 10:01 AM
I'm building a website which catalogues a number of products. Each product has a unique code.

Just now, this is how I'm doing it:

http://www.mywebsite.com/generic.html#pr=productcode

"generic.html" is a page containing only the stuff universal to every product - the page structure, the necessary JS files, the link to a CSS file, etc... When the page loads, a JS function called "InitialiseStateFromURL" is called. It gets the window.location, extracts the product code and requests the information for the product using AJAX. The various elements in the generic page then get filled with the product's information.

However, I'd like the URLs to be even simpler, like this:
http://www.mywebsite.com/productcode
The trouble is that doesn't point to a webpage file. It points to a non-existent folder. Now, for every product, I could create a folder with an htaccess file redirecting to a URL of the form mentioned at the start... but I suspect there's a better way to do it.

Can anyone tell me?

Thanks,
Seymour.

¥åßßå
12-08-2008, 11:40 AM
I'd advise that you use a url prefix to make your life easier in the long run, ie/ http://domain.com/product/product_id

htaccess ( without url prefix ) :
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* /generic.php [QSA, L]

htaccess ( with url prefix ) :
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/product/
RewriteRule /product/(.*)$ generic.php [QSA,L]

ish :P

¥

Seymour Clufley
01-22-2009, 10:43 AM
Yabba (or anyone else!),

I'm trying to do mod_rewriting now but unfortunately your code isn't working...

As a simple test, here's what I want to do. Change:
http://www.mysite.com/sub/test.htmlto:http://www.mysite.com/sub2/test.htmlSo all we need to do is replace "sub" with "sub2". Can anyone tell me how to do that?

Here's the contents of my htaccess file:
DirectoryIndex cover.html
AddType text/x-component .htc


RewriteEngine On

RewriteCond %{REQUEST_URI} ^/sub/
RewriteRule /sub/(.*)$ sub2 [QSA,L]The first two lines are for something else and they work as desired. But the rest don't!

Thanks for any help,
Seymour.

bradenkeithcom
01-22-2009, 01:07 PM
take out ^/sub/
It's telling it that sub2 is located at /sub/sub2 instead of /sub2

I think that'll fix your problem... not totally confident with apache... visit this article for extensive coverage of how to rewrite urls: http://www.addedbytes.com/apache/url-rewriting-for-beginners/