Forum Moderators: phranque
I have the following rule in my .htaccess:
RewriteRule ^catalog/([^/]+)/([0-9]+)/([0-9]+) products.php?category=$1&page=$2&per_page=$3 [QSA,L]
It works great.
On my page I have a form with the following action:
<form action="" method="GET">..............
When submitting it, I get the following:
[mysite.com...]
Instead, I need to redirect to:
[mysite.com...] which will in turn execute that rewrite rule that I have.l
Also, "mysite.com" always changes, so I cannot be specifying it in my .htaccess.
How do I go about redirecting GET (or POST) requests?
Thanks.
When submitting it, I get the following: http://example.com/catalog/1/6/?category=0&page=2&per_page=6Instead, I need to redirect to: http://example.com/catalog/2/6/
When you say "I get the following," I suspect you mean that you see that query-string URL in your browser address bar.
If this is the case, then it's likely too late to do anything about it -- Your site is "publishing" that URL - "defining" it on the Web as "the URL" for the page, and redirecting it afterward is simply going to slow down the user experience.
The proper approach is to modify the form to use POST only, and to link to the page in the SEO-friendly URL format. Once this is done, you may optionally wish to include code in your .htaccess file to clean up search results where these dynamic URLs have already been indexed. But adding a redirect without correcting the root problem of that unfriendly URL getting published really doesn't help anything.
Having fixed the URL at its source, the code to fix-up search engine listings and recover old bookmarks has to be constructed in such a way as to avoid interacting with your existing friendly-to-unfriendly internal rewrite. We do this by making sure that the unfriendly URL is being directly requested by the HTTP client (browser or 'bot), and not as the result of your pre-existing internal rewrite. So, the code looks like this:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /products\.php\?category=([0-9]+)&page=([0-9]+)&per_page=([0-9]+)\ HTTP/
RewriteRule ^products\.php$ http://example.com/catalog/%1/%2/%3? [R=301,L]
It's possible that you've got an error in an existing RewriteRule that is appending the query string to the rewritten friendly URL and then invoking an external redirect. This can be caused by using the [QSA] flag when it is really not needed, or by forgetting to clear the query string by appending a "?" to the substitution URL in a RewriteRule (as shown in the rule I posted here).
It is also possible that your existing code is incorrect because it is generating an external redirect when an internal rewrite is desired. Be sure you understand the difference between these two functions.
See this thread in our Apache Forum Library for more information: Changing Dynamic URLs to Static URLs [webmasterworld.com]
Jim