Welcome to WebmasterWorld Guest from 18.206.194.83

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod_rewriting query strings

     
5:29 pm on Nov 13, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 28, 2003
posts:90
votes: 0


I need a push to get this type of URL rewritten:
/index.php
/index.php?st=x
/index.php?st=x&ct=y
/index.php?st=x&ct=y&id=z

I want it accessed as:
/index.html
/index.html/x
/index.html/x/y
/index.html/x/y/z

I'm reviewing the mod-rewrite docs I can find and trying to make sense of it all but certain thing dont do anything and others give me a 404.

Do I have this backwards or what?

RewriteEngine On
RewriteRule ^index\.html$ /index.php #this rule works when I hit index.html
RewriteRule ^/(.*)$?state=$1
RewriteRule ^\/(.*)\/(.*)$?state=$1&city=$2

5:38 pm on Nov 13, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


This thread is definitely a good place to start

An Introduction to Redirecting URLs on an Apache Server [webmasterworld.com]

6:19 pm on Nov 13, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


micahb37,

You'll need to use RewriteCond [httpd.apache.org] %{QUERY_STRING} to extract variables from the query string and insert them into the substitution URL. Create back-references using parenthesized subexpressions, just as in RewriteRule, and then reference them using "%1", "%2", etc. in the substitution URL.

Jim

8:08 pm on Nov 13, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 30, 2003
posts:8
votes: 0


Sorry for the mis-post up there. That orginal question was mine, my partner was logged into my system, but I've tried your suggestion and it still rewrites my first rule correctly but I'm still having a problem with the second rule:

RewriteEngine On
RewriteCond ${QUERY_STRING} ^(.*)
RewriteRule ^index\.html$ /index.php
RewriteRule ^index\.html$ /index.php?state=%1

am I on the right track or getting colder? This particular change just causes my server to hang. Nothing happens. I don't expect for anyone to write the code for me but this is all pretty new to me and good examples seem to help until I fully understand how all this works. I'll keep trying a few things here to see if I can get past this pesky issue.

9:04 pm on Nov 13, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


carbonspace,

Looks like you need to know that a RewriteCond applies only to the first RewriteRule following it, unless you chain the rules. Therefore, your RewriteCond applies only to your first rule, which I suspect is not what you want.

It also looks like you want to rewrite from static, search-engine-friendly URLs to a query-string-format needed by your script, rather than the other way 'round. So let's take these static URLs as an example, and feed the "subdirectory names" to a php script as query string variables:

/index.html
/index.html/state
/index.html/state/city
/index.html/state/city/zip

We want to rewrite all of these to index.php, passing the other parameters in the query string.


RewriteRule ^index\.html/([^/]+)/([^/]+)/([^/]+) /index.php?state=$1&city=$2&zip=$3 [L]
RewriteRule ^index\.html/([^/]+)/([^/]+) /index.php?state=$1&city=$2 [L]
RewriteRule ^index\.html/([^/]+) /index.php?state=$1 [L]
RewriteRule ^index\.html /index.php [L]

If all variables are *not* present in the requested URL, the first rule will fail, and the URL will then be tried against the second rule. Again, if variables are missing that rule will fail. Finally, if no variables are present in the requested URL, then we just rewrite from index.html to index.php.

The regular expression "([^/]+)" means "find one or more characters that are not a slash, and remember them for later use: "[^/]" means "not a slash", "+" means "one or more" of the preceding, and putting the whole mess inside parentheses allows the matched characters to be back-referenced by "$1", "$2", etc. on the right side of the Rule.

Hope that helps. Check out the links in this Introduction to mod_rewrite [webmasterworld.com] post, and dig into some of the archived posts here for more example of static->dynamic URL rewriting.

Jim

10:50 pm on Nov 13, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 30, 2003
posts:8
votes: 0


You rock, jd! Its no wonder everyone in this forum speaks highly of you. Thanks for breaking this down for me. That really helped clarify alot of things and since this is really my first day working with rewrite rules, I'm sure I'll get the hang of this fairly soon with your help and some practice.

Now I'm on the hunt to remove the .index.html from the string and just have it accessed as site.com/state/etc.

Thanks again. Have a great day!

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members