Hi Daniweb,

I've got a question regarding what I call "dynamic URL rewriting" -- which is probably the wrong term -- and need some help.

OK, so basically, I know how to rewrite the URL for static pages (i.e. have "localhost/index.php" also work as "localhost/index". However, let's say I have three database categories that each serve different users: "Sports", "Education", and "Finance". Their URL's are as such:

http://localhost/category/index.html


What I'd like is for all of these "folders" to dynamically be updated with one line of RewriteRule code. (something like this down below) That way, if I add 100 categories, I won't have 100 lines of "RewriteRule ..." to add.

I've seen that "environment variables" might do that trick but I'd REALLY prefer not to go that route. Rather, I'm hoping there are existing mod_rewrite functions up to that task.

An example would be Wordpress. If you create a category for each subject you want to write about and turn on permalinks ("pretty links"), you'll notice how they rewrite the URLs. I'm guessing they don't actually write RewriteRule(s) dozens of times...

Recommended Answers

All 3 Replies

You can do something like this
User accesses http://www.somewebsite.com/category/baseball
.htaccess

RewriteEngine on     
RewriteRule ^ category/([^/.]*)/?([^/.]*)/?$ /displaycategory.php?category =$1 [L]

Then on the displaycategory.php page do a simple get

$category = $_GET['category'];

You could then use $category to lookup the information that you need to display. In my previous link $category would equal baseball.

Thanks a bunch, sops21, for your response.

Yeah, I figured it out within a couple hours of posting but forgot to mark solved. I'm using REGEX to help me create the "pretty links".

I have tried the following code (it was mentioned above). I am getting some issue in it.
When I am opening http://somesite.com/category, it is not opening anything.
When I am opening http://somesite.com/category/, it is not showing images and CSS file seems not to be there.

After I open the source code, it shows the css file path as css/style.css, but when I save this page. Then there is no style.css instead there is style.htm.

After viewing the code of style.htm it shows the path of file to be category/css/style.css whereas if it would have been css/style.css then it would have been fetched correctly.

You can check it out at: http://smstongue.com/category/
1.
RewriteEngine on
2.
RewriteRule ^ category/([^/.]*)/?([^/.]*)/?$ /displaycategory.php?category =$1 [L]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.