Forum Moderators: phranque
www.mysite.com/template.php?id=pagetitle
www.mysite.com/template.php?id=folder/pagetitle
I want the URLs to look like:
www.mysite.com/pagetitle
www.mysite.com/folder/pagetitle
Basically, I want to rid the URL of the
template.php?id= part. How do I write this RewriteRule into my
.htaccess file? Or, is there a better way. I originally wrote a separate rule for every single URL, but, as you could imagine, the performance of the site took a huge hit. (see example below).
RewriteEngine On
RewriteRule ^$ www.mysite.com/template.php?id=home
RewriteRule ^(about/history/地bout/history)$ template.php?id=about/history
RewriteRule ^(services/存ervices)$ template.php?id=services
RewriteRule ^(contact/圭ontact)$ template.php?p=contact
etc., etc., etc.
And I thought, "There must be a way to write one or two rules that would cover all these URLs." Any help would be great.
Also, I'm a newbie to code -- design's my gig -- so directing me to the Apache docs will not help me much, unfortunately (as it's Greek to me). Of course, I will continue to read the Apache docs and try to get it. And I will continue to search around for tutorials.
Perhaps an example, or citing an article with examples would help me.
Cheers.
There's a post in our Apache forum library that specifically addresses this problem -- publishing static links and then rewriting those static URLs to dynamic URLs when requested from your server, which is what you'll need to do.
Jim
I read your wonderful article "Changing dynamic to static URLs." The selection was very useful and reinforced some important concepts I have previously learned.
I do understand how to write a rule for
/title/ to be sent to the server as template.php?id=title, however, this would still require me to write a similar rule for every URL on the site. My question: is there one or two rules that I can write in
.htaccess to apply to all of the URLs? I apologize if I'm being dense. I really did read all of the linked docs and the Apache docs.
For example, the rule:
RewriteRule ^([a-z]+)\.php$ /template.php?id=$1 [NC,L]
But there are several problems. First, this rule would also rewrite template.php to itself, with "p=template". A RewriteCond exclusion will be needed to prevent this recursion.
And second, mod_rewrite has no way to tell if the name of your variable should be "id" or "p" because there is no apparent fixed association. So, you'd have to handle that based on a group of specific URLs or query values.
Two simplifications you can definitely use is to make the trailing slash optional instead of duplicating every URL-path pattern with and without a slash, and to combine lines with the same parameter name (here, "id="). For example:
RewriteRule %{REQUEST_URI} !^/template\.php$
RewriteRule ^(about/history存ervices)[b]/?$[/b] /template.php?id=$1 [L]
RewriteRule ^(about/history/地bout/history)$ template.php?id=about/history
RewriteRule ^(services/存ervices)$ template.php?id=services
Hopefully, that will help.
Jim
[edit] Code correction [/edit]
[edited by: jdMorgan at 11:00 pm (utc) on Nov. 23, 2006]