Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite help please!

         

jayskates

12:00 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



Hi everyone,

I'm currently working on a pretty dynamic website... I'm trying to use htaccess to rewrite all the ugly urls into nice pretty ones!

I have the most basic rule working...

RewriteRule (.*)\.html$ index.php?p=$1


which is rewriting a url like
http://www.buschsystems.com/busch-new/index.php?p=Promotions


to
http://www.buschsystems.com/busch-new/Promitions.html



now here is where I'm stuck, I also want to rewrite url's that contain a second get variable,

Example:

http://www.buschsystems.com/busch-new/index.php?p=prod&name=Product-Name


or
http://www.buschsystems.com/busch-new/index.php?p=About&subPage=Testimonials



How can I write these to be
http://www.buschsystems.com/busch-new/prod/Product Name.html


or
http://www.buschsystems.com/busch-new/About/testimonials.html



Any help would be greatly appreciated! I have a deadline to meet (today) and would love to not miss it haha.

Thank you in advance!

wilderness

12:07 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



recent thread nearly identical [webmasterworld.com]

There's also a recent thread which provided insight into second strings.

jayskates

12:11 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



Thanks for the reply wilderness, I check out that thread and it didn't really help me...

Another problem i'm having is trying to create different rules using mixes of the same variables. ie.

RewriteRule ^(.*)/(.*)$ index.php?p=$1&name=$2
RewriteRule ^(.*)/(.*)$ index.php?p=$1&subPage=$2

When I do this, the rewrite's stop working all together.

Thanks!

g1smd

6:34 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never use multiple .* patterns.

The .* says put all the input in here. The second .* says after you have put "everything" into $1 put "everything" that follows in $2. How can "everything" be followed by "everything".

The .* is greedy, promiscuous and ambiguous.

Add the [L] flag to every rule.

Use
RewriteRule ^([^/]+)/([^/.]+)$ index.php?p=$1&name=$2 [L]


This rule will capture all URL requests like
example.com/something/something


If you have similar URLs that need different treatment the RegEx pattern must be adjusted to match the right requests. Perhaps some have a particular leading character or consist of a particular number of characters?

There are ten threads with similar discussion this week, and more than 5000 in this forum in the last decade.