I have set up a URL rewrite which works fine when I manually set or type in the URL. But when I use a link in the page within a <cfoutput> tag for example, the link duplicates the string.
I'm using ColdFusion 2016 with IIS 10
For example the URL is
www.site.com/results.cfm?lang=1&categoryid=2&budegtid=all&typeid=all&propertyid=316
and I want it outputted like
www.site.com/properties/en/all/all/all/316
it works but what seems to be happing when I set the link using...
<cfoutput> using the query...
<cfif getProps.recordcount>
<cfoutput query = "getProps" startrow="#url.start#" maxrows="#perpage#">
View
</cfoutput>
the result in the link on the page outputs as www.site.com/properties/en/all/all/all/316/properties/en/all/all/all/316
I have web/config set up as
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<rewrite>
<rules>
<rule name="Property list URL rewrite">
<match url="^property/([_0-9a-z-]+)/([_0-9a-z-]+)/budget-range-([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="resultsproperty.cfm?lang={R:1}&categoryid={R:2}&budegtid={R:3}&typeid={R:4}&propertyid={R:5}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<httpErrors errorMode="Detailed" />
</system.webServer>
</configuration>
The ColdFusion cfquery is as follows...
<cfquery name="getProps" datasource="#session.odbcname#">
SELECT *
FROM properties P
INNER JOIN areas A
ON A.area_id = P.property_areaid
INNER JOIN categories C
ON C.category_id = P.property_propcategoryid
INNER JOIN price R
ON R.price_id = P.property_regularity
INNER JOIN types T
ON T.type_id = P.property_proptypeid
WHERE P.property_status != 'Off Market'
<cfif url.ref is not "all">
AND
(
property_ref like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#url.ref#%">
OR property_refid like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#url.ref#%">
OR property_town like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#url.ref#%">
)
</cfif>
ORDER BY P.property_refid
It seems to be pulling in the actual URL first and then adding on the rewrite.
Any help much appreciated.
You need to start the href with a forward slash to indicate it is from root. The href is resolved/interpretted in the browser. Not starting with a forward slash tells the browser that the url is relative to the current path.
If you are at http://example.com/admin/ and have an href logout for example it will be resolved as http://example.com/admin/logout. However if you have an href /logout it will resolve to 'http://example.com/logout.
As your own answer stated, forcing an absolute path works and can be preferred sometimes but sometimes not. Make sure you understand the underlying issue.
Solved! by pre setting the URL root in the coldfusion application file i.e. <cfset request.root = 'http://www.example.com/'> and then reference the #request.root# var within the link, it uses the link correctly and dose not duplicate the url. So this is a cold fusion issue rather than a URL ReWrite issue.
Related
I do my best to scan the forum for help to make a web.config to do a Rewrite of this kind of url
domain.com/default.asp?id=3&language=2
My hope is that this can be
domain.com/en/service
where language=2 is "en"
and id=3 is page "Service" (this name exist in a mySQL)
I can only find example that do it vice versa...
Like this
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^enquiry$" />
<action type="Rewrite" url="/page.asp" />
</rule>
</rules>
</rewrite>
I would like it to be something like this... I know this isn't correct, but maybe explains my problem.
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^default.asp?id=3&language=2$" />
<action type="Rewrite" url="/en/serice" />
</rule>
</rules>
</rewrite>
If you want to use regular expressions you could do something like this
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.asp?language={R:1}&id={R:2}" />
</rule>
This would rewrite "domain.com/en/service" as "domain.com/default.asp?language=en&id=Service", or "domain.com/2/3" as "domain.com/default.asp?language=2&id=3"
To change the 2 to en and the 3 to service, along with all the other options though I think you would need a separate rule for each permutation, or have some sort of logic within your asp pages to read your querystring variables and send the corresponding values to your SQL queries. Note also that the parameters in the friendly url appear in the same order and the querystring variables in the rewritten URL, although this shouldn't really be an issue. If someone tries to access the page with the original "unfriendly" url they will find what they are looking for, whichever way round they enter the querystring variables.
Please note, I didn't actually hand code the rule above, I generated it with the URL Rewrite module in IIS manager - it makes life a lot easier
Also note, as discussed with my namesake in the other answer, this only applies to IIS7 and above
I have done this in Classic ASP using custom error pages and this seems to be the best way unless you use some sort of third party component installed on the server.
To do this, in IIS (or web.config) you need to set up 404 errors to go to a specific custom error Classic ASP page (eg. 404.asp).
In this custom error page you first need to check to see if the URL is valid. If it is you can Server.Transfer to the correct page, return a 200 response code, and parse the URL there to convert the URL to the values needed for the database lookup, etc. If it's not a valid URL then you show a custom error page and return a 404 response code.
The code to check for a valid URL and to retrieve the URL parameters will vary greatly depending on your URL structure. But to find the URL requested on the custom 404 error page you have to look at the querystring, which will be something like "404;http://domain.com:80/en/service/".
Here's some sample code that gets the parameters from the requested URL:
Dim strUrl, intPos, strPath, strRoutes
strUrl = Request.ServerVariables("QUERY_STRING")
If Left(strUrl, 4) = "404;" Then
intPos = InStr(strUrl, "://")
strPath = Mid(strUrl, InStr(intPos, strUrl, "/") + 1)
If strPath <> "" Then
If Right(strPath, 1) = "/" Then strPath = Left(strPath, Len(strPath) - 1)
End If
strRoutes = Split(strPath, "/")
'Here you can check what parameters were passed in the url
'eg. strRoutes(0) will be "en", and strRoutes(1) will be "service"
End If
And here's how you can setup custom error pages in the web.config (rather than in IIS):
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" responseMode="ExecuteURL" path="/404.asp" />
</httpErrors>
</system.webServer>
</configuration>
I'm using the following rule in my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rewrite">
<match url="([0-9]+)/([0-9]+)" />
<action type="Rewrite" url="index.php?year={R:1}&item={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
It works.
But I need to show a 404 error if user writes one or more letters in the second query (item), e.g. 1b, or 1bla, etc.
It should only be allowed a (positive) number and, possibly, a query like ?utm_source=anothersite or ?ref=anothersite etc.
In other words, url should be:
www.mysite.com/{year}/{item}
where {year} and {item} are both mandatory and could be numbers only, no other elements allowed (e.g.: www.mysite.com/{year}/{item}/{somethingelse}), except ?key=value.
How can I change match url value in order to achieve that result?
Thanks.
Please add a "$" to the end your pattern.
<match url="([0-9]+)/([0-9]+)$" />
This is my tested result:
I am totally new to IIS servers and need to implement some rewrites.
The following rule in my RewriteMaps file causes a 500 error:
<add key="/contenttemplates/news.aspx?id=8589976277&LangType=3081&CurrencyType=156" value="/" />
While this one works fine:
<add key="/offset-air.php" value="/" />
I can only assume the query string has something to do with the issue, but I struggle to find a reason as to why this URL would be an issue. Initial google attempts have come up empty.
I would be very grateful if someone could point me in the right direction.
Many Regards!
If your rewrite maps are causing you issues, you should consider encoding your urls correctly.
An & in the URL (in either the key or the value) should be replaced with an &.
<add key="/contenttemplates/news.aspx?id=8589976277&LangType=3081&CurrencyType=156" value="/" />
would then become:
<add key="/contenttemplates/news.aspx?id=8589976277&LangType=3081&CurrencyType=156" value="/" />
I'm trying to use an URL Rewrite on an IIS 8.0 to rewrite existing URL:s on a developer machine. The reason for this is that I don't want to change existing (old) code.
What I'm trying to achieve is to change the following code in the response stream:
Foo Page
into:
Foo Page
But when I'm trying, I end up with:
Foo Page
And as you all know, this is not a very satisfying result.
So - how do I do this rewrite proper to achieve what I'm trying to do?
I know there are better ways of doing this, using application variables etc., but it's an old solution and I don't want to mess too much with the application itself. I want to keep the changes to a minimum. At least to begin with.
The rules I tried look like this:
<system.webServer>
<rewrite>
<outboundRules>
<rule name="foo.com" enabled="true">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="foo.com" />
<action type="Rewrite" value="foo.localhost" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
I guess there is some regex magic I should be using.
Your rule needs to be changed to:
<rule name="foo.com" enabled="true">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^(.*)foo.com(.*)$" />
<action type="Rewrite" value="{R:1}foo.localhost{R:2}" />
</rule>
In the pattern, ^(.*) will match anything (0 or more characters) from the beginning before foo.com and (.*)$ anything after foo.com until the end.
You can then use the back references in the action, where {R:1} will take the value matching (.*) before foo.com and {R:2} the value matching (.*) after foo.com
I use the following code to redirect to my home page on login... now i want to go a step further and add a logic where it redirects to different page based on user type.
for eg: if a user type is employee then i should redirect to employeehome.xhtml and so on ... is this possible ?
<page xmlns="http://jboss.com/products/seam/pages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.2.xsd">
<navigation from-action="#{identity.login}">
<rule if="#{identity.loggedIn}">
<redirect view-id="/Home.xhtml" />
</rule>
</navigation>
I suppose you have a login.xhtml page from which the user logs in.
Then you can create a login.page.xml page containing some navigation rules. For example:
<navigation from-action='#{identity.login}'>
<rule if="#{identity.loggedIn and s:hasRole('management')}">
<redirect view-id="/management/home.xhtml"/>
</rule>
<rule if="#{identity.loggedIn and s:hasRole('upload')}">
<redirect view-id="/secure/upload.xhtml"/>
</rule>
<rule if="#{identity.loggedIn and (s:hasRole('sss') or s:hasRole('sssmgmnt'))}">
<redirect view-id="/secure/sss/home.xhtml"/>
</rule>
<rule if="#{identity.loggedIn}">
<redirect view-id="/secure/home.xhtml"/>
</rule>
</navigation>
next, you can restrict the pages, so only users with the right role can go there. In my pages.xml, I have the following lines:
<page view-id="/secure/upload.xhtml" login-required="true">
<restrict>#{s:hasRole('upload')}</restrict>
</page>