Forum Moderators: phranque
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST}!^www\.mysite\.com
RewriteRule ^(.*)$ [mysite.com...] [R=permanent,L]
# Skip the next two rewriterules if NOT a spider
RewriteCond %{HTTP_USER_AGENT}!(msnbot¦slurp¦googlebot) [NC]
RewriteRule .* - [S=2]
#
# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)&osCsid=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) $1?%1&%2 [R=301,L]
#
# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)&osCsid=[0-9a-z]+$¦^osCsid=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) $1?%1 [R=301,L]
RewriteRule ^(.*)coupons/(.*)$ $1coupons.php?cName=$2&cId=$3 [L,NC]
the purpose, as I mentioned above, is to change/coupons.php?cName=category-name&cId=#*$!
to
/coupons/categroy-name/
Well, I was hoping for more than a repeat of that - It's rather ambiguous. Many people get this rewriting thing backwards. If your scripts output a dynamic link like "/coupons.php?cName=category-name&cId=123" and you really serve static pages, then you can rewrite the dynamic link to a static page using:
RewriteCond %{QUERY_STRING} ^cName=([^&]+)&cId=([^&]+)$
RewriteRule ^coupons\.php$ /coupons/%1?cId=%2 [L]
Going the other way, rewriting a static link of /coupons/gategory-name/ to "/coupons.php/category=category-name&cId=123" will be impossible in this case, unless you want the cId to be a fixed number; The cId information has to be provided in the input URL, or you can use a fixed value, but it has to come from somewhere.
This second case here is the more commonly-used: In order to present search-engine-friendly URLs on their pages, most webmasters modify their scripts to output static-looking links like "/coupons/gategory-name/123" on their pages, and then rewrite those links, when requested by a client, to the dynamic form like "/coupons.php/category=category-name&cId=123" needed to call the script again.
Assuming that your script is modified to output static links like "/coupons/gategory-name/123" and you want to rewrite those URLs, when requested by a browser or spider, to call your script again with "/coupons.php/category=category-name&cId=123", you'd use:
RewriteRule ^coupons/([^/]+)/(.+)$ /coupons.php?cName=$1&cId=$2 [L]
It's a bit quiet back here on weekends, so hopefully you can reassess your plan and adapt one of the code samples above to meet your actual needs.
P.S. It's fun to see my old osCommerce code posted again. :)
Jim
I was kind of confused. what is actually the difference between situation 1 and 2?
I have a dynamic site, if I do nothing, the url is
/coupons.php?cName=category-name&cId=123
I would like to turn it into something like
/coupons/category-name/
so it is search engine and user friendly. It sounds like it falls into situation 2, right?
So, they will then request these friendly URLs from your server. As soon as the request is received and processed through httpd.conf, your .htaccess code will modify the URL-path so that your script(s) can be succesfully invoked with the same type of URL as before.
The key here is that mod_rewrite is called and modifies URL-paths after a request is received, but before any content is served or any scripts are invoked. It therefore functions as an "input URL changer" and not as an "output URL changer." And that's why lots of people confuse the "direction" of the function and the from-to of the URL-path rewriting.
Another key issue that I touched on before is that all the information needed to construct the "real" URL must be available in the "friendly" URL; mod_rewrite does text-substitution, and not database lookups. So, you can change long variable names to short, change underscores to hyphens or slashes, etc., but you must leave enough information in the friendly URL to guarantee an unambiguous result when rewriting back to the longer query-string form needed to invoke your script(s).
It's unlikely that any code posted here will be exactly right for your site. Our goal here is to help you get started and to support your efforts, not to act as a free code-writing service. Otherwise, the demand for help will far outstrip the available supply -- everyone here on WebmasterWorld is a volunteer. So, it's pretty much a given that you'll have to modify any examples you find here now or later. That's because it's almost impossible to give an exact description of what is needed, so it's impossible to give an exactly-correct solution. And also, once you see what *can* be done, it's likely you'll want the rules to do more. Quite conveniently, there are some citations of and links to useful references in our forum charter [webmasterworld.com]. ;)
Jim