How to work with virtual (seo) URLs?
| From: | Erik Gyepes | Date: | Sat, 18 Nov 2006 23:05:01 +0000 |
| Subject: | How to work with virtual (seo) URLs? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-244840@lists.php.net to get a copy of this message | ||
Hi folks,
my another question is how to work with URLs like below in PHP:
http://www.example.com/section/
http://www.example.com/section/subsection/
http://www.example.com/section/subsection/subsubsection/
http://www.example.com/section/subsection/subsubsection/ ....
These URLs works very well with this mod_rewrite rules:
----
RewriteEngine On
#check if file or directory real exists:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#do the rule only if the address has no extension:
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
#replace /whatever to /whatever/, not apply to whatever/!
RewriteRule ^(.+[^/])$ /$1/ [R]
#do the rule only if the address ends with trailing slash:
RewriteCond %{REQUEST_URI} ^.*/$
#if the rule ends with trailing slash then redirect to index.php..
RewriteRule ^(.*)/$ /index.php?p=$1 [L]
----
So Apache stage is solved, but how about the PHP stage?
The problem is that there can be many of subsections and how I can know that for example /animals/ is a subsection of /photogallery/ and not /profile/ ???
[url]http://www.example.com/photogallery/animals/[/url] - GOOD
[url]http://www.example.com/profile/animals/[/url] - BAD
How do you solve these things?
One idea which I had is to store sections in array an then compare them with the section's name in URL, but I can't get it work for more nested URL's.
Any ideas or web resources are appreciated!
Cheers.