How to rewrite urls that have an accidental %20 at the end? - mod-rewrite

I wound up with a bunch of backlinks to my site that have the correct URL, except there was an accidental trailing space so the links came out with a trailing %20, which causes a page not found error.
I tried variations on this:
RewriteRule ^/%20 /
but it's not working.
Is a RewriteCond statement also needed?
Please note this is an IIS 6 server, and these are Wordpress pages that I'm linking to.
Somebody please tell he the secret code to get rid of a %20 at the end of a URL.
Thanks!

You may permanently redirect all concerned urls having a trailing %20 to the same url without the trailing %20 by using the following rule:
If you use UrlDecoding Off in your iirf.ini, use:
RedirectRule (.*)%20$ $1 [R=301]
Otherwise, IIRF automatically decodes the URL for you before trying to apply the rules. You can therefore use:
RedirectRule (.*)\s$ $1 [R=301]
In order to test this using testdriver.exe:
Put the above rule in a file called iirf.ini.
Create a file called SampleUrls.txt containing some test URLs, for example:
/ NO REWRITE
/%20 REDIRECT 301 /
/article NO REWRITE
/article%20 REDIRECT 301 /article
Call testdriver with a command similar to %iirfpath%\testdriver.exe -d .
Please note: testdriver does not decode URLs.
You should get an output similar to the following (I removed some newlines):
TestDriver: linked with 'Ionic ISAPI Rewriting Filter (IIRF) 2.1.1.28 x64 RELEASE'.
TestDriver: The IIRF library was built on 'Aug 8 2011 02:26:29'
Processing URLs...(.\SampleUrls.txt)
***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)
NO REWRITE '/' ==> --
OK
***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)
REDIRECT 301 '/%20' ==> '/'
OK
***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)
NO REWRITE '/article' ==> --
OK
***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)
REDIRECT 301 '/article%20' ==> '/article'
OK
0 Errors in 4 Total Trials

Related

rewrite rules with apache does not work

I try to make specific rewrite rules by .htaccess but i need help, its doesn't work.
This is my url :
http://mywebsite.net/aaa-bbbb/cccc
aaa = dynamic parameter ( [a-z] )
bbb = dynamic parameter ( [a-z] )
cccc = dynamic parameter but optionnal ( [a-z] )
And i want rewrite user to this url :
http://mywebsite.net/mydir/ccc?x=aaa&y=bbbb**
url still will http://mywebsite.net/aaa-bbbb/cccc on tabbar
i try this
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ mydir/$3 [L]
but it doesn't work
thx !
To rewrite a request to
http://mywebsite.net/aaa-bbbb/cccc
internally to
/mydir/ccc?x=aaa&y=bbbb
you need such a rewrite rule:
RewriteEngine on
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ /mydir/$3?x=$1&y=$2 [L]
Note: the above rule has the syntax to be used inside the http servers host configuration. If you really want to use a .htaccess style file instead you need to use a slightly modified syntax, since those files work on relative paths:
RewriteEngine on
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ mydir/$3?x=$1&y=$2 [L]
You can spot the missing / in the rewrite goal. The .htaccess style file has to reside inside the folder holding the mydir folder for this to work. Also the interpretation of such files has to be enabled at all in the http server configuration for.
A general note: .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server for nothing. They are only offered as a last option for those not having access to the http servers host configuration. So for example for cheat shared hosting providers. In general you should always prefer to place such rules inside the host configuration itself.

mod_rewrite - replace word or character in url

Am trying to mod_rewrite a URL, but unfortunately without any luck.
http://mywebsite.com/gallery/mycustom-gallery/linkid417
Should be changed to:
http://mywebsite.com/gallery/mycustom-gallery/#417
Where '417' is a dynamic id of an image & mycustom-gallery will also change every-time.
I've tried the following rules, but none seems to work...
RewriteRule ^/gallery/$1/#([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/y/([0-9]+)$ /gallery/$1/linkid [L,R=301]
RewriteRule mycustom-gallery/#.*$ /mycustom-gallery/linkid=417/$1
Regards,
Charl
If you are trying to rewrite from http://mywebsite.com/gallery/mycustom-gallery/linkid417 to http://mywebsite.com/gallery/mycustom-gallery/#417 you can do it as follows:
RewriteRule ^/gallery/([a-zA-Z0-9_-]+)/linkid([0-9]+)$ /gallery/$1/#$2 [NE,L,R=301]
The NE|noescape flag prevents Apache from escaping the hash in the URL during the 301 redirect.
If however you wish to do the opposite, i.e. redirect from http://mywebsite.com/gallery/mycustom-gallery/#417 to http://mywebsite.com/gallery/mycustom-gallery/linkid417, that cannot be done, as the fragment part of the request ("#417") is not passed to Apache along with the request as per RFC1808.

How to make Isapi Rewrite rules case-sensitive?

I want to direct my users based on a case-sensitive url:
www.mysite.com/a ==> page 1
www.mysite.com/A ==> page 2
I'm using the ISAPI rewrite with the following rule:
RewriteRule ^([0-9a-zA-Z] {1,7}) $/redirect/?K=$1 [L]
Apparently this rule is not case-sensitive, since it redirects to the same page. What's is wrong?
=====UPDATE=====
I solved in part this problem by adding a binary query (case-sensitive) in my MySql statement. But in chrome this problem still occurs.
I see 2 problems:
1) space between [] and {}
2) your regular expression is non-case sensitive
for lowercase expression you need RewriteRule ^([0-9a-z]{1,7}) $/redirect/?K=$1 [L]
and for upper-case RewriteRule ^([0-9A-Z]{1,7}) $/redirect/?K=$1 [L]

How do I use mod_rewrite to redirect a request for a non-existent file?

I have an external application (Gobi) which generates links to records in our library catalog based on the ISBN. The application can be configured to use an URL of my devising, such as:
http://library.example.edu/isbn/$ISBN
It's then supposed to replace the $ISBN bit with the actual ISBN, for an URL like this:
http://library.example.edu/isbn/9780262516778
And sometimes it actually does that. But other times, it takes my lovely clean URL and turns it into something like this (split out onto separate lines for your convenience):
http://library.example.edu/isbn/$isbn
?sid=ybp.com:GOBI2
&genre=book
&title=EVOCATIVE+OBJECTS%253a+THINGS+WE+THINK+WITH
&isbn=9780262516778
&date=2007
&volume0
&stitle=
These links then fail with a 404 error, because there is no file on our server named isbn/$isbn. The nice short URLs are handled by mod_rewrite using the following:
RewriteRule ^isbn/([0-9]+)/?$ http://catalog.library.example.edu/vufind/Search/Home?lookfor=$1&type=isn&library=XMPLE [NC,R=301,L]
Now I need to come up with a second rule to handle the ugly long variant. So far I've come up with this:
RewriteCond %{QUERY_STRING} ^sid=ybp\.com:GOBI2&genre=book&title=(.*)&isbn=([0-9]*)&date=([0-9]*)&volume0&stitle=$
RewriteRule ^isbn/\$isbn.+ http://library.und.edu/test.php?lookfor=%2&type=isn&library=UNDAL [NC,R=301,L]
This fails, generating an error message File does not exist: /var/www/isbn. I think what is happening is that the RewriteRule needs a real file there, but I'm not sure.
Suggestions?
It fails because the RewriteRule fails to match the given long URL. So, the URL passes through as un-rewritable and then Apache falls back to the default behavior of looking for a directory or file named 'isbn/$isbn' on the file system.
You should really enable rewrite logging to see these issues in your log. You can enable logging by adding this to the first line of your .htaccess:
RewriteEngine On
along with something like this in your httpd.conf or apache2.conf:
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9
When you do this, you should see something like in the rewrite.log file:
127.0.0.1 - - [01/Dec/2011:23:00:13 +051800] [susam/sid#7fa14866fe98][rid#7fa1484d80a0/initial] (3) [perdir /home/susam/www/susam.in/] add path info postfix: /home/susam/www/susam.in/isbn -> /home/susam/www/susam.in/isbn/$isbn
127.0.0.1 - - [01/Dec/2011:23:00:13 +051800] [susam/sid#7fa14866fe98][rid#7fa1484d80a0/initial] (3) [perdir /home/susam/www/susam.in/] strip per-dir prefix: /home/susam/www/susam.in/isbn/$isbn -> isbn/$isbn
127.0.0.1 - - [01/Dec/2011:23:00:13 +051800] [susam/sid#7fa14866fe98][rid#7fa1484d80a0/initial] (3) [perdir /home/susam/www/susam.in/] applying pattern '^isbn/\$isbn.+' to uri 'isbn/$isbn'
127.0.0.1 - - [01/Dec/2011:23:00:13 +051800] [susam/sid#7fa14866fe98][rid#7fa1484d80a0/initial] (1) [perdir /home/susam/www/susam.in/] pass through /home/susam/www/susam.in/isbn
Note the last but one line. The path is just isbn/$isbn. The query is not a part of the path. So, The .+ you have added in the regex ^isbn/\$isbn.+ is causing the problem.
Now, how do we fix it?
RewriteCond %{QUERY_STRING} ^sid=ybp\.com:GOBI2&genre=book&title=(.*)&isbn=([0-9]*)&date=([0-9]*)&volume0&stitle=$
RewriteRule ^isbn/\$isbn http://library.und.edu/test.php?lookfor=%2&type=isn&library=UNDAL [NC,R=301,L]
Note that this time the regex is just ^isbn/\$isbn.

Rewriting URLs and "faking" folders

I'm trying use mod_rewrite to rewrite URLs from the following:
http://www.site.com/one-two-file.php
to
http://www.site.com/one/two/file.php
The folders don't exist, but "virtually" exist for the rewriting purpose.
What rule do I used in this?
Untested:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.php$ $1-$2-$3.php [L]
I can't really understand your explanations about virtuality and existence: one-two-file.php must exist or you'll have nowhere to redirect to.
Update
The previous version works fine when used from an .htaccess file. However, if used from main http.conf file you need to add leading slashes:
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)\.php$ /$1-$2-$3.php [L]
I presume that's why it wasn't working for the OP (he was probably getting a 404 not found status code).

Resources