Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite in .htaccess help

Content negotiation, regular expressions, rewrite rules

         

brad05

6:13 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



I've got URLs that look like:

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.

jdMorgan

7:05 pm on Nov 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com]?

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

brad05

8:08 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



Thanks, Jim. I'll read those selections.

brad05

8:50 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



Dear 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.

jdMorgan

9:48 pm on Nov 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Review the other citations in the charter, and especially concept of a "back-reference" in the mod_rewrite documentation.

For example, the rule:


RewriteRule ^([a-z]+)\.php$ /template.php?id=$1 [NC,L]

would rewrite *all* .php URLs containing only alphabetic characters to template.php?id=<URL-path>.

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]

is equivalent to your original

RewriteRule ^(about/history/地bout/history)$ template.php?id=about/history
RewriteRule ^(services/存ervices)$ template.php?id=services

and prevents the rewrite loop and handles both example "id" URLs, with or without trailing slash.

Hopefully, that will help.

Jim

[edit] Code correction [/edit]

[edited by: jdMorgan at 11:00 pm (utc) on Nov. 23, 2006]

brad05

10:53 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



First, thank you so much for all of your help. I really appriciate the time you've spent on my questions.

Oops -- a mistake in my original post: all URLs are

id=
.

I will continue to read, experiment, using the information you have provided.

Thank you.