I’m new to mod_rewrite but already like what it can do with it! I just want to make sure I have it right before I go LIVE and search engines pay a visit.
Trying to come up with as useful and “pretty” URL’s for my content module in the CMS I use.
My goal was to be able to reach main categories in the CMS with typing the name of the category without any numbers or file extensions.
I wanted that a URL like: mydomain.com/trails could be promoted (for instance in magazine advertising) and accessed.
So far I have achieved this effect but I have certain reservations to how “proper” it is.
This is what I have in my .htaccess file:
#->TRANSLATE Categories automatically (exclude all other modules)
RewriteCond %{REQUEST_URI} !.(shop|sponsors|modules)
RewriteRule ^(/)*([/a-zA-Z_]+)(/)*(\.htm)?$ modules/toc/index.php?toc_topic_name=$2 [L]
Hence I am first excluding some directories from the mod_rewrite rule…Then I am looking for any folder looking structure or possible a file with the extension “.htm”. I plan to make the categories .htm files and this will be the link structure (but access per folder is important in promotion sense).
So now the Trails category can be shown properly typing in the example URL I presented earlier. The link will be defined as mydomain.com/trails.htm and also works fine.
In my modules/toc/index.php file I use the toc_topic_name to query the database for a match. Since I realized a match might not be found I put a re-direct to either the home page or a search page if the category wasn’t found.
If there were a duplicate match I would also re-direct the user to another “help type” page.
I also took in consideration that deeper category hierarchies might not query the DB properly because of the string variations…so I have taken multiple measures to always assure I have a clean string and the last category to use in the query. Hence I can also get a URL like: mydomain.com/trails/bikes/mountain to show the proper content of “mountain”. Plus I have a "purdy" URL. 🙂
I guess my question is: Is my Rewrite rule ok? And is it based on too loose rules?
I’m also wondering if I would be better off by adding a number to my rewritten links as to assure I get a unique link. Ex: mydomain.com/trails19.htm
I just think the URL looks prettier without the number. 🙂
Also, does search engines read content of a link in a directory like:
mydomain.com/trails (since it doesn't in reality have a index file) or do they prefer the file extension type: mydomain.com/trails.htm
I also thought instead of mydomain.com/trails19.htm I could do a variation of:
mydomain.com/19/trails.htm but then it might fool the user thinking there is a directory called 19…
Would greatly appreciate any feedback!
Thanks in advance!