开发者

apache mod_rewrite: using database to update rewrite rules

开发者 https://www.devze.com 2023-01-28 10:03 出处:网络
Total newbie at mod_rewrite. Let\'s say I want to create nice URLs for every manufacture开发者_StackOverflow社区r on my site,

Total newbie at mod_rewrite.

Let's say I want to create nice URLs for every manufacture开发者_StackOverflow社区r on my site, so I have www.mysite.com/samsung www.mysite.com/sony www.mysite.com/acme

works well enough.

However, if I have hundreds of manufacturers and if they're changing constantly, what then? There are some vague references for something called rewrite map somewhere but nothing that explains it and no tutorials. Can anyone help?

Also, why is this problem not the main topic covered in tutorials for mod_rewrite? How is mod_rewrite possibly useful when you have to maintain it manually (assuming you have new content on your site once in a while)?

There is also mention of needing to have access to httpd.conf How do I access httpd.conf on my hosting provider's server? How does every other site do this?

Thanks


Just came across this answer while searching for a similar solution — searching a bit further I discovered that mod_rewrite now has the RewriteMap directive, which will do exactly what you want without the need to run PHP or another scripting language.

It lets you define a mapping rule with a text file, a DBM file, an external script or an SQL query.

I hope that helps!


The way this would typically be done is that you would take all URLs that match a specific pattern and route them to a PHP file (or whatever your server-side programming language is) for more complex routing. Something like this:

RewriteRule ^(.*)$ myroute.php?url=$1 [QSA,L]

Then, in your myroute.php file, you can include logic to look at the "url" query string parameter, since it will contain the original URL that came in. Perhaps you could match it to a manufacturer in the database, or whatever else is required.

This example obviously takes all URLs and maps them to myroute.php. Another example might be something like:

RewriteRule ^/manufacturers/(.*)$ manuf.php?name=$1 [QSA,L]

In this case, it will map URLs like so:

/manufacturers/sony => /manuf.php?name=sony
/manufacturers/samsung => /manuf.php?name=samsung
etc...

In this case, your manuf.php file could look up the database based on the name query string parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号