I was wondering if there is a way to handle in a rewrite rule ( iis and apache ) url query strings which contain a slash ( %2F ) as part of it.
as an example:
www.domain.com/project/word1
gets rewritten to
www.domain.com/project/index.php?word=word1
via this rule ( in iis ):
<rule name="Friendly">
<match url="^(.+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?word={R:1}" appendQueryString="false" />
</rule>
or in apache:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?word=$1
this works correctly.
however there are cases like this :
www.domain.com/project/word1%2Fword2
which should be redirected to
www.domain.com/project/index.php?word=word1/word2
but obviously i get an error 404 because of the slash ( %2F ). Is there any way to solve this ? Even if it meant I have to cut off the /word2 part and redirect www.domain.com/project/word1%2Fword2 to www.domain.com/project/index.php?word=word1
thank you in advance
I find your case peculiar because in Apache's mod_rewrite module (I'm not sure about IIS) it's stated that RewriteRule patterns are matched against the, I quote, "(%-decoded) URL-path (or file-path, depending on the context) of the request".
What I'm experiencing though after testing is that the encoded slashes are not decoded or not interpreted by the server (I too get an 404 error).
However, I realize one mostly url encode URL parts when the content is to be used as a query string argument (for example http://www.example.com/?path=word1%2Fword2) which is logical because you don't want the server to interpret the encoded slash as a part of the URL path. Note that this observation is contradictive to the statement above so take it with a grain of salt.
Solution
What I can confirm however is that it's no problem rewriting www.domain.com/project/word1/word2. Therefore I suggest that you don't encode the pat of the URL that will be used in the path part, but possibly whitelist allowed characters instead so you avoid special characters like ?.
Test results
.htaccess:
RewriteRule ^(.+) index.php?word=$1 [L]
index.php:
<pre>
<?php var_dump( $_GET, true ); ?>
</pre>
URL: http://test/word1%2Fword2
Yields 404.
URL: http://test/word1/word2
Yields:
<?php
array (size=1)
'word' => string 'word1/word2' (length=11)
?>
Related
I have a web server that hosts multiple domains running IIS 8.5. One of those domains is going away and needs to be redirected to the root of another domain on the server. I created the rule below and it works fine as long as there is not path. If there is a path, it appends the path and results in a 404 error. I want to ignore the path and only redirect to www.DomainB.com. I would think path would only be included if I put in http://www.DomainB.com/{R:1} as the redirect url but this doesn't seem to be the case. Where am I going wrong here?
<rule name="Redirect DomainA.com">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*DomainA.com$" />
</conditions>
<action type="Redirect" url="http://www.DomainB.com/" redirectType="Found"/>
</rule>
I figured it out. The above is actually good. It was another rule for DomainB, one set to do a redirect from www to non www, which was causing the issue. It did not have DomainB in the condition, but rather a reg expression that matched any www and that rule does have the {R:1} on it, so the path was actually being appended by that rule after the DomainA.com rule processed. After making that rule DomainB specific, the above Redirect DomainA.com rule worked as is
I've unfortunately got a lot of pages SEO-indexed with "/home" as prefix.
Now I need to change to prefixless urls in Piranha, which is easy. But is there a place where I can force redirects from old urls to new urls?
E.g. redirect "www.example.com/home/page" to "www.example.com/page". The problem is that the old page doesn't exist anymore so I can't put a script on that page to do the redirect.
Best regards
Lars, Denmark
I think the best way is to use url rewrite module (need to install it from web platform).
In web.config You can append rewrite rules to system.webServer => rewrite => rules.
I will not test it, but it should be something like this:
<system.webserver>
<rewrite>
<rules>
<rule name="RedirectToPrefixless" stopProcessing="true">
<match url="^home/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>
Tag "match" finds for urls begining with "home/" - part of regex "(.*)" create group which will be used in redirect url
Tag "action" states, that for matching URL it should be redirected to "(.*)" part from "match" part.
So for example:
/home/test1 redirects to /test1
/home/test2?page=22 redirects to /test2?page=23
All redirects are "Permanent" (http status 301) - it can be also changed ;)
I am moving a site from IIS 7 to Apache 2.4 and have the following web.config rewrite rule I am having trouble converting to .htaccess. The rule essentially allows for clean (seo friendly) urls by rewriting all files without an extension with the .cfm extension (e.g. www.mydomain/bag rewrites on the server as www.mydomain.com/bag.cfm. The working rule in web.config is shown below
<rule name="Rewrite all non extension requests to .cfm" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.cfm" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{R:1}.cfm" />
</rule>
I have tried all the like posts I could find on here and unfortunately none of them worked for me. I am running Lucee 5.0 on CentOS 7 (Apache 2.4) if that matters. Any guidance would be greatly appreciated.
I don't really know about Tomcat; I assume you need Apache because Tomcat's no good for serving your non-script content.
You could set them up so that they both have the same document root, thus have congruent URLs, but have Tomcat listening on a different port, and not have that port open externally. Then you could proxy requests that are for (hidden) cfm files like so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.cfm -f
RewriteCond %{THE_REQUEST} ^\S++\s++([^?\s]++)(\?\S*)?
RewriteRule ^ http://127.0.0.1:8080%1.cfm%2 [NS,NE,P]
RewriteRule (?<=.cfm)$ http://127.0.0.1:8080/404.cfm [NS,NE,P]
The second rule is an example to pretend that the cfm files are not there if request directly.
You will need the appropriate proxy module(s) enabled.
I would like to convert this web.config
<rule name="cambiarPass" stopProcessing="true">
<match url="^cambiarPass/" />
<action type="Rewrite" url="modulos/cambiarPass/controller.php" appendQueryString="false" />
</rule>
to .htaccess. Please help!
Bear in mind that I do not use IIS but the provided code seems pretty self-explanatory in terms of how it should be converted to an Apache rewrite rule.
The <match url="^cambiarPass/" /> line is set to apply URL rewriting only to URLs (paths) which begin with cambiarPass/. The
<action type="Rewrite" url="modulos/cambiarPass/controller.php" appendQueryString="false" />
line is the one doing the rewriting and redirecting all matched URLs to modulos/cambiarPass/controller.php. The appendQueryString attribute is obviously a synonym for the Apache QSA rewrite flag meaning the rewrite process will discard and ignore any existing query string data during the rewrite. The stopProcessing attribute seems to be yet another equivalent for the Apache L rewrite flag meaning if this rule is matched any additional rewrite rules that might follow the current rule will simply be ignored.
Here is the complete code.
RewriteEngine on
RewriteRule ^cambiarPass/ modulos/cambiarPass/controller.php [L]
I saw some examples on how to convert IIS to htaccss.
I'm using httpd conf (for performance reasons) + i used online converter for IIS --> htaccess.
when converting this rule:
<rule name="videorecords URL" patternSyntax="Wildcard">
<match url="videorecords/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="videorecords/{R:1}" />
</rule>
to:
#RULE VIDEORECORDS URL
RewriteRule videorecords/* videorecords/$1 []
the apache doesnt load and i see in the logs:
Syntax error on line 211 of httpd.conf: RewriteRule: unknown flag ''
Your flags are blank: []. So you need to either add a flag in ther (like L) or remove the square brackets.
Though, I don't think that the rewrite rule online converter gave you is actually what you want.