Rewrite Rule with optional parameter iis7.5

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Rewrite Rule with optional parameter iis7.5

07 May 2013, 04:41

Hi there

I have a rewrite rule setup for the news section of our site. The rule is as follows:

Code: Select all
Rewriterule ^/motorcycle_news/categories/(.*?)/news_(\d+)_(.*)\.html$ /newsItem.asp?cat=$1&cpID=$2&page=$3 [NC]


This will rewrite links like this:

Code: Select all
http://www.phmotorcycles.co.uk/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-“very-important”.html


This works fine however when it comes to links from places like twitter they place tracking code after the .html as follows:

Code: Select all
http://www.phmotorcycles.co.uk/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-%E2%80%9Cvery-important%E2%80%9D.html?utm_source=dlvr.it&utm_medium=twitter


Of course this doesn't work because of the $ after the .html in the rewrite rule. However I have been given the following code to try, which I thought might work but doesn't:

Code: Select all
RewriteCond %{QUERY_STRING} ^(utm_source=.*&utm_medium=.*)?$ [NC]
Rewriterule ^/motorcycle_news/categories/([^/]+)/news_(\d+)_(.*)\.html$ /newsItem.asp?cat=$1&cpID=$2&page=$3 [NC]


Does anybody have any clues on how I can fix this?

Many thanks in advance for any help.

Graham

User avatar
Posts: 28
Joined: 02 Mar 2012, 12:50

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 06:17

I guess you are looking for [QSA] flag

QSAppend or QSA — appends query string of the original request to the Substitution string (regardless of whether Substitution has new query string or not). It may be useful when you need add new query string parameters but also preserve the originally requested ones

Please try

RewriteRule ^/motorcycle_news/categories/(.*?)/news_(\d+)_(.*)\.html$ /newsItem.asp?cat=$1&cpID=$2&page=$3 [NC,QSA]

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 07:08

Hi Sergey,

I have tried this but it doesn't work unfortunately. The people who tweet our news articles on our website want to be able to track where users are coming from hence appending to the original URL. The problem I have been faced with is that, if I remove the $ from after the .html part of the rewrite rule then of course it works. However the problem with this is that each of the url's also fail on with PCI compliance because cross scripting code can also be added to the URL and can be executed.

Therefore I only want to be able to add Google's tracking code to the links only so they would be considered optional:

Code: Select all
http://www.phmotorcycles.co.uk/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-%E2%80%9Cvery-important%E2%80%9D.html(?utm_source=dlvr.it&utm_medium=twitter)


Cheers
Graham

User avatar
Posts: 28
Joined: 02 Mar 2012, 12:50

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 07:25

Could you please show rewrite.log for
http://www.phmotorcycles.co.uk/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-%E2%80%9Cvery-important%E2%80%9D.html?utm_source=dlvr.it&utm_medium=twitter
request

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 08:12

I am not sure what is going on but I cannot get the rewrite.log file to be written to. I have setup httpd.conf and the error.log file is showing but even though the directive to write to the rewrite.log file is in place it does not seem to work.

Do you have any thoughts on this?

Sorry Graham

User avatar
Posts: 28
Joined: 02 Mar 2012, 12:50

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 09:18

Please try to grant NTFS write permissions for rewrite.log file for Everyone and perform iisreset

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 10:16

Ok, that worked thank you. Here is the log for a successful transaction:

Code: Select all
217.32.220.226 217.32.220.226  Tue, 07-May-2013  14:14:41 GMT [www.phmotorcycles.co.uk/sid#6][rid#23887320/initial] (3) applying pattern '^/motorcycle_news/categories/([^/]+)/news_(\d+)_(.*)\.html$' to uri '/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-�very-important�.html'
217.32.220.226 217.32.220.226  Tue, 07-May-2013  14:14:41 GMT [www.phmotorcycles.co.uk/sid#6][rid#23887320/initial] (1) Rewrite URL to >> /newsItem.asp?cat=motorcycle-parts&cpID=801580922&page=rossi-jerez-is-�very-important�
217.32.220.226 217.32.220.226  Tue, 07-May-2013  14:14:41 GMT [www.phmotorcycles.co.uk/sid#6][rid#23887320/initial] (2) rewrite '/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-�very-important�.html' -> '/newsItem.asp?cat=motorcycle-parts&cpID=801580922&page=rossi-jerez-is-�very-important�'


Of course trying to run it with the tracking details on the end of the URL doesn't work and so of course nothing logs at all.

Many thanks for all your help.

Graham

User avatar
Posts: 28
Joined: 02 Mar 2012, 12:50

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 10:37

Seems request interrupted before ISAPI_Rewrite.

You can use "Failed request tracing" tool in Iis for debug this issue, please provide us with this logs

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 11:00

I have done that and the XML log is attached to this post.

I hope this helps.

Cheers
Graham
Attachments
failedRequestLog.txt.zip
(7.84 KiB) Downloaded 781 times

User avatar
Posts: 28
Joined: 02 Mar 2012, 12:50

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 11:10

We need freb.xsl too, please zip all folder with log

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

07 May 2013, 11:21

Sergey, hi

Many thanks here are the required files.

Cheers
Graham
Attachments
FailedRequestLog.zip
(23.01 KiB) Downloaded 770 times

User avatar
Posts: 28
Joined: 02 Mar 2012, 12:50

Re: Rewrite Rule with optional parameter iis7.5

08 May 2013, 08:26

Sorry but I can't see target url, only
http://www.phmotorcycles.co.uk:80/usedbikes/10128/[l]/images/biketraderimages/{ds::imgfile}

please request
http://www.phmotorcycles.co.uk/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-%E2%80%9Cvery-important%E2%80%9D.html?utm_source=dlvr.it&utm_medium=twitter

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

08 May 2013, 11:10

Sergey, good afternoon

Hope you are well. Please find attached the failed request log.

Cheers for all your help.

Graham
Attachments
FailedRequestLog.zip
(23.41 KiB) Downloaded 773 times

User avatar
Posts: 3
Joined: 09 May 2013, 08:28

Re: Rewrite Rule with optional parameter iis7.5

09 May 2013, 08:47

Will this work?

Code: Select all
Rewriterule ^/motorcycle_news/categories/(.*?)/news_(\d+)_(.*)\.html(\?(.*))?$ /newsItem.asp?cat=$1&cpID=$2&page=$3(?4&$4) [NC]

User avatar
Posts: 8
Joined: 07 May 2013, 04:34

Re: Rewrite Rule with optional parameter iis7.5

09 May 2013, 09:42

Phil, hi

YOU ARE AN ABSOLUTE ******************************* STAR!

Thank you so much, that worked a charm.

Cheers
Graham

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 20 guests