How to use ISAPI_rewrite to convert pretty URLs into query string URLs? - isapi-rewrite

In ISAPI_rewrite 3 documentation is this example
RewriteRule ^(.*?\.asp)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,LP,QSA]
http://www.example.com/foo.asp/a/A/b/B/c/C
http://www.example.org/foo.asp?a=A&b=B&c=C
Since my products can have more or less parameters in query string, this seemed like right approach. Based on given example I tried to create a rule for my case with no luck.
http://www.example.com/product-name.aspx/a/A/b/B/c/C
http://www.example.org/products.aspx?a=A&b=B&c=C

After more digging I came up with solution. Seems like there is no other way but using two rules.
RewriteRule ^(.*?\.htm)/([^/]*)/([^/]*)(.+)? $1$4?$2=$3 [NC,LP,QSA]
RewriteRule ^.*?\.htm$ product.aspx [NC,L]

Related

Rewriting an URL to become a query string

I'm trying to rewrite URLs such as
/product/16/var1/value1/var2/value2...
to this
index.php?page=product&id=16&var1=value1&var2=value2...
In other words, I would like to have a "main parameter" translated to an id (and I can do this), but I would also like to have, from that point on, couples of "directories" translated recursively to key-value pairs.
Is this possible with Apache mod_rewrite?
In the absence of the [L] flag, any mod_rewrite rule will apply repeatedly to any URI which corresponds to the rule's rewrite conditions and pattern.
Knowing this, we can build a mod_rewrite rule which looks for any URIs with query strings beginning in a certain way and then repeatedly harvests the folder-names of that URI (two at a time) to build the rest of the query string.
See example below:
In the root folder of
http://example.com/
save an .htaccess file with the following mod_rewrite directives:
RewriteEngine On
RewriteRule ^(product)/([0-9]{2})/(.*) http://%{HTTP_HOST}/$3/index.php?page=$1&id=$2
RewriteCond %{QUERY_STRING} ^(page=product&id=[0-9]{2}.*)
RewriteRule ^([^/]+)/([^/]+)/(.*/)?index.php$ http://%{HTTP_HOST}/$3index.php?%1&$1=$2
Using the above:
http://example.com/product/16/var1/value1/var2/value2/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2
and
http://example.com/product/16/var1/value1/var2/value2/var3/value3/var4/value4/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2&var3=value3&var4=value4

Simple mod_rewrite rules not working

In trying to get my head around mod_rewrite, I've added a simple rewrite rule:
RewriteRule ^z$ z1.html
The idea is to let someone access www.mysite.com/z and have them get www.mysite.com/z1.html
But this doesn't work. I have to change it to
RewriteRule ^/z$ /z1.html
But I'm not seeing anything in the mod_rewrite rules that requires a "/" in front of the terms, so why doesn't the first one work?
A bit more complicated, I have the rule
RewriteRule ^/([^.]+)$ /1$.html
and this one doesn't work either, even with the "/" characters.
The idea here is to let some enter www.mywebsite.com/z1 and have it become www.mywebsite.com/z1.html.
Does anyone see the problem?
Thanks.
If you have mod rewrite rules in vhost/server config, it requires a / as part of the pattern. The second rule doesn't work because you want $1, and not 1$:
RewriteRule ^/([^.]+)$ /$1.html [L]

mod_rewrite, change one URL with query to a completely different URL

I am migrating data from one content management system to another. There is no relationship between old URLs and new URLs, although both contain query strings. I am trying to set up a set a rewrites that will redirect broad category lists of data from one to the other.
Here's a sample:
OLD
rss.php?categoryID=53
NEW
index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19
I tried
RewriteRule ^rss.php\?categoryID=53 index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
but it doesn't match. If I follow that one with
RewriteRule ^rss.php index.php?module=news&type=user&func=list&tid=1 [L]
if DOES match, so I conclude that the question mark in the old URL is causing the problem. I am already escaping the question mark. What do I do?
I will probably end up with about 50 of these in my .htaccess file.
You can't match against the query string (all that stuff after the ?) in a RewriteRule, you need to use a RewriteCond and match against the `%{QUERY_STRING} var:
RewriteCond %{QUERY_STRING} ^categoryID=53$
RewriteRule ^rss\.php$ /index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
Or change the brackets to [R=301,L] if you want to redirect the browser.

Remove part of a URL

How can I remove main/ in http://domain/main/about
so the URL will be like this http://domain/about
Thanks
Well, in the absence of any further details, you could try something like this:
RewriteRule (.*)/main/(.*) $1/$2
But this is a VERY broad rule - you'd probably want to tune it more tightly than that. Try turning the RewriteLogLevel to 9 and watching the rules as they get processed so you can get a better idea of what is being matched.
If you only need one file to be rewritten :
RewriteRule main/about about [L]
or else the TML answer is good :
RewriteRule (.*)/main/(.*) $1/$2 [L]
It rewrites any URL with a /main (even in third, fourth, ... places) in it to prefix/suffix URL.

Mod_rewrite help

I'm trying to remove query strings from my calendar, but my mod_rewrite is not appending the query string.
The website is http://cacrochester.com/Calendar
and if you click the link to go to a different month, the query string is usually http://cacrochester.com/Calendar?currentmonth=2010-11
With my rule below, it just doesn't append the query string so when you click the next month link, it just stays on the month October. What's wrong with my rule?
Here is my rule
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^.*$ http://cacrochester.com/Calendar? [NC,R=301,L]
EDIT:
What i want is to take a url like http://cacrochester.com/Calendar?currentmonth=2010-11 and turn it into something like http://cacrochester.com/Calendar/2010-11
You probably need your app to output relative urls like "/Calendar/2010-11". That's a simple code change.
Then in Apache you'd want to rewrite those urls, using:
RewriteRule ^/Calendar/([0-9]+-[0-9]{2})$ /Calendar.php?currentmonth=$1 [NC,QSA,L]
(You don't want a RewriteCond for this rule.)
Forcing a redirect with R=301 will only expose the internal url scheme. I don't think that's what you want.
To maintain query strings when rewriting, use the QSA (query string append) flag.
[NC,R=301,QSA,L]

Resources