Forum Moderators: phranque
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
An Introduction to Redirecting URLs on an Apache Server [webmasterworld.com]
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
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.
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]
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
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!