Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite question - code needs streamlining

mod rewrite question - code needs streamlining

         

richiebman

2:20 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Hello everyone. I currently have the following code in my .htaccess file:

RewriteRule ^home_(([a-z]¦[-])+)$ index.php?pages=home&theme=$1
RewriteRule ^services_(([a-z]¦[-])+)$ index.php?pages=services&theme=$1
RewriteRule ^recent-work_(([a-z]¦[-])+)$ index.php?pages=recent-work&theme=$1
RewriteRule ^vision_(([a-z]¦[-])+)$ index.php?pages=vision&theme=$1
RewriteRule ^contact_(([a-z]¦[-])+)$ index.php?pages=contact&theme=$1
RewriteRule ^t-and-c_(([a-z]¦[-])+)$ index.php?pages=t-and-c&theme=$1
RewriteRule ^site-map_(([a-z]¦[-])+)$ index.php?pages=site-map&theme=$1

and am trying to cut it down to one line but can't work out how. Any ideas? (the above code works well also) What I think should work is something like this:

RewriteRule ^(([a-z]¦[-])+)_(([a-z]¦[-])+)$ index.php?pages=$1&theme=$2

But it doesn't and I'm no expert at this. Does it have anything to do with the '_' character separating the 2 expressions? If so what would be a valid substitute? When I have tried the above code and I echo $_GET["theme"], very bizarrely it only echos the last letter of the string stored in $_GET["pages"]? No idea why and now I have to go to bed.

Thanks for your time,
R

jdMorgan

2:33 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the underscore to parse out the two variables:

RewriteRule ^([^_]+)_(.+)$ /index.php?pages=$1&theme=$2 [L]

You may well need to add exclusions using RewriteCond if any of your other page/image/script filenames have an underscore in them; Otherwise, they will be rewritten to index.php.

Jim

richiebman

11:01 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Excellent. Worked first time. One day I'll hopefully get to grips with this and stop having to bother you JDMorgan :). You have sorted it once again. Many thanks.
R