Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite help

simple rule help

         

catchmeifucan

1:20 am on Mar 12, 2005 (gmt 0)

10+ Year Member



Hi, I am rewriting this url
/coupons.php?cName=pc-software&cId=134

to

/coupons/pc-software/

this is the rule I used,

#Change coupons.php to coupons/category-name/
RewriteRule ^(.*)coupons/(.*)$ $1coupons.php?cName=$2&cId=$3 [L,NC]

It just didn't work.

Anybody help?

jdMorgan

3:57 am on Mar 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We'll need a little background information to begin...

Where did you place this code, in httpd.conf or in .htaccess?
Is there any other mod_rewrite code in that file? If so, does it work?

Most importantly, what are you trying to accomplish by doing this rewrite?

Jim

catchmeifucan

5:05 pm on Mar 12, 2005 (gmt 0)

10+ Year Member



I put it into the .htaccess file, the other rewrite code are:

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]

catchmeifucan

5:06 pm on Mar 12, 2005 (gmt 0)

10+ Year Member



the purpose, as I mentioned above, is to change

/coupons.php?cName=category-name&cId=#*$!

to

/coupons/categroy-name/

jdMorgan

9:54 pm on Mar 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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]

I left the cId in the output query string because you did not specify what you wanted to do with it. If you want to discard it, simply end the substitution URL after the "?".

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

catchmeifucan

4:56 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



Hi, Jim,

Thanks a lot. But the rule still won't work. Half a year a ago I had a coder worked on a 404.php page and did the rewrite there. I am not sure if there's some kind of confilt in his code with the rules set up in .htaccess file.

catchmeifucan

5:08 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



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?

jdMorgan

6:23 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, this is the more usual situation. Modify the scripts (you can use preg_replace in PHP) so that the script outputs SE-friendly URLs. This is what users and search engines will see, click on, and index.

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