Correct extra spaces in URL with .htaccess - .htaccess

Can .htaccess be used to correct extra space errors in URL encoded links?
For example:
Incorrect: http://%20www.google.com/
Correct : http://www.google.com/

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\%20$ $1 [NC]
Something to that effect should work, don't count me on it.
Regex will help you.
http://corz.org/serv/tricks/htaccess2.php
If you have captured all your error pages, you could write something in the server language of your error page e.g. error.php and get that to do the string edit for you rather than rely on htaccess, as you may not want to really replace all spaces with nothing as files can have spaces in their name. String comparisons are expensive so may be best to only do it if there is an error, depends on your needs.

I have concluded that removing the leading space is not possible though .htaccess or error pages. Trying to access that page will return a Error 105 (Unable to resolve the server's DNS address). Therefore, .htaccess or a server error page will have no effect as the client cannot reach the server.

Related

ERR_TOO_MANY_REDIRECTS htaccess

Could anyone can explain me what this .htaccess code doing?
RewriteRule ([0-9]+)/([^t]{1}[A-Za-z0-9_-]{0,})\.([^s]{1}[A-Za-z0-9]+)$ ../items/go.php?u=$1/$2.$3
I have a folder : mywebsite.com/download/folder1/ with a .htaccess file in the uplevel mywebsite.com/download/ folder contains code above.
When I access a file directly to the path mywebsite.com/download/folder1/file.mp4 it returns error :
mywebsite.com redirected you too many times. Try clearing your
cookies. ERR_TOO_MANY_REDIRECTS
I tried to clear my browser cookies but the issue persist.
This issue is solved when I commented out the htaccess line above.
This rule appears to be looking for a specific pattern consisting of:
<numbers>/<alphanumeric string not starting with t and with other conditions applied>.<alphanumeric string not starting with s>
then redirecting the user's browser to go.php with the parsed filename passed as file arguments.
ERR_TOO_MANY_REDIRECTS is likely due to an infinite redirect loop. Without knowing what other RewriteRule statements are present it is difficult to narrow down an exact cause. You can narrow it down further on your end by seeing if the rewritten output might match any other RewriteRule conditions.
In general, the rule looks suspicious for the example input given. If this is home-grown I would suggest rewriting it entirely, as its current form will given unexpected outputs for user-generated data that could cause any security holes present in the go.php script to be easily exploited / exposed.

htaccess - how to cut tailing number for URI and save it in a Cookie

I am completely lost and all my searching on Google and winthin the board did not get me to a working solution.
Setup
Apache-webserver
htdocs are located: xampp/htdocs
my Joomla site is located: xampp/htdocs/example.com/www
Two kinds of URI as possible on our homepage:
URI end with a number (8 digits; e.g.: www.example.com/[any page/12345678)
URI does not end with a number (e.g.: www.example.com/[any page])
If the URI ends with a number do the following
Read the number from the URI and place it in a cookie
Redirect the request to the same URI except for the number
(e.g.: www.example.com/[any page])
If the URI does not end with a number do the following
Open the page as requested (e.g.: www.example.com/[any page])
I was hoping that this was not too difficult to do but I am completely lost. Would you please as to give me a little help. Please do include a statement to RewriteBase as well because it is giving me a hard time, too.
RewriteEngine On
RewriteRule (.*)/([0-9]{8})$ /$1 [R=301,CO=cookieName:$2:.example.com:1440:/]
take a look at http://httpd.apache.org/docs/current/rewrite/flags.html#flag_co for more information about cookie flag

htaccess redirects that ignore query string

I've recently inherited a webserver whose IP previously belonged to a well known band's forums.
Problem is I'm now drowning in 404 errors!
The URLs in question look like this:
http://[server_ip]/forum/ucp.php?mode=register&coppa=0
http://[server_ip]/forum/viewtopic.php?f=3&t=45330
http://[server_ip]/forum/index.php+++++++++++++++++++++++++++++++++++++++++++++++Result:+%ED%E5+%...
http://[server_ip]/forum/viewtopic.php?f=3&t=44597&start=0
In an ideal world I would like to redirect any traffic going to /forum/ucp.php, /forum/viewtopic.php or /forum/index.php elsewhere regardless of query string.
Or anything going to /forum/.* elsewhere, if that's doable.
I've tried a number of different solutions with little success, any help appreciated.
Assuming that you want to redirect all traffic to /forum/.* to http://mysite.com/somedirectory, which you can replace with the actual URL you wish to redirect to, you can add the following to the .htaccess in the root directory of your sites domain.
RewriteEngine On
RewriteBase /
#for any request starting with forum
RewriteRule ^forum/ http://mysite.com/somedirectory? [NC,L,R=302]
Be sure to keep the ? if you want to remove the query string params from the original query. If you want to make it a permanent redirect, change the 302 to a 301.
Don't really know what you have tried so far but this site will probably help you.
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#redirects

How to redirect an erroneous URL

I just noticed that sometimes (even when given a wrong url) load perfectly fine. How do they accomplish this? What I mean is, suppose you click on a link that seems good like www.foo.com but it contains in the end a space character which would appear on the address bar as www.foo.com%20 some sites manage to redirect this to their correct url while others just break. How can this be achieved? I'm guessing it's something to do with the .htaccess but I have no idea what to do or where to do it.
The URL I'd like to redirect looks like this actually: http://foo.com/%C2%A0
I get the following error message:
The requested URL /%C2%A0 was not found on this server.
How can I make this redirection?
So far I came up with:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://www.foo.com/ [R=301,L]
but it's not working at all
URL Rewrite would be the IIS version that may exist in other forms if you want to look at re-writing the URL assuming you mean this kind of case.
Don't forget that browsers may make certain guesses about what someone enters so that if someone types in "foo.com " that the browser may trim white space by default rather than URL encode the text. If "http://foo.com" fails then it may try "http://www.foo.com" for another idea as these could be seen as simple interpretations to take on what someone types in. If both fail then it may just Google the text believing that the address bar should be treated like a search box.

RewriteRule does not match as expected (by me)

i cannot understand why this is not working. Maybe somebody can explain it to me.
I have the following:
http://www.mydomain.de/my-apache-tag.html
which should rewrite to
http://www.mydomain.de/tag/my-apache
In my .htaccess I enabled this rule:
RewriteRule ^/(.*)-tag\.html$ /tag/$1
In my understanding everything between / and before -tag.html should be taken as argument 1.
But the above will lead to a 404. I even put the domain in front of /tag/$1 but this doesn't work too.
Any help is appreciated, thanks much.
Christian
Depending on apache version, the leading slash is not required. (You can place /? if you want to be able to match both cases) see #Jeff Parker comment.
Also, if you want the URL to be displayed as http://www.mydomain.de/tag/my-apache in the browser when typing http://www.mydomain.de/my-apache-tag.html, you should add the [R] flag to redirect.
And of course, your file on the server should be name my-apache in the tag/ directory. Or you will have 404 error.
If you want the client to type http://www.mydomain.de/tag/my-apache and be served http://www.mydomain.de/my-apache-tag.html your rule is wrong and should be :
RewriteRule ^tag/(.*)$ /$1-tag.html
If you want to combine both behaviour, be careful of loops.

Resources