htaccess read whitespaces "was not found on this server" - .htaccess

I have a little problem. I'm using htaccess for more userfriendly url's but when i add some whitespaces in URL it gives me the next error:
The requested URL /capitole/Limba Engleza was not found on this server.
My htaccess code looks like that:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule nota.jpg php/img_nota.php [R=301]
RewriteRule ^login login.php
RewriteRule ^recuperare recuperare.php
RewriteRule ^inregistrare inregistrare.php
RewriteRule ^/?([\sa-zA-Z0-9_-]+)(/?([a-z0-9=]+)(/=)?([a-z0-9=]+)?)?$ index.php?page=$1&par1=$3&par2=$5 [NC,L]
</ifModule>
Can someone help me?

The URL can not contain whitespaces. Whitespaces in URLs are encoded using the percentage (%).
If it is a query string (after ?) it's common to use the operator + (for example: big+ships).
If the string is in the path of the URL, then %20 is used (for example: big%20ships).
As detailed on the RFC: rfc2396
The space character is excluded because significant spaces may
disappear and insignificant spaces may be introduced when URI are
transcribed or typeset or subjected to the treatment of word-
processing programs. Whitespace is also used to delimit URI in many
contexts.
space = US-ASCII coded character 20 hexadecimal

your 2nd segment doesn't accept space modify it to read as follows.
RewriteRule ^/?([\sa-zA-Z0-9_-]+)(/?([\sA-Za-z0-9=]+)(/=)?([a-z0-9=]+)?)?$ index.php?page=$1&par1=$3&par2=$5 [NC,L]

Related

How can I Rewrite Query String to Path and remove %20 in url with dash (-)? [duplicate]

So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:
http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny
I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:
http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny
So, it needs to be able to account for multiple spaces. Any questions? :)
Use these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
This will replace all space characters (\s or %20) to hyphen -
So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301
Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.
Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.
Building on #anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:
Options FollowSymlinks MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.

htaccess Rewriterule for multiple Parameters

I have an URL that Looks like:
https://www.example.com/project/api/read.php?uid=1234567&dvc=ABCDE
I would like to establish a rewrite rule that turns the URL in:
https://www.example.com/project/api/read/1234567/ABCDE
My try Looks like this:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^read/([0-9a-zA-Z_-]*)([0-9a-zA-Z_-]*)$ read.php?uid=$1&dvc=$2 [NC,L,QSA]
RewriteRule ^read/([0-9a-zA-Z_-]*)([0-9a-zA-Z_-]*)$ read.php?uid=$1&dvc=$2 [NC,L,QSA]
You are missing a slash between the path segments in the RewriteRule pattern.
Assuming your .htaccess file is located inside the /project/api subdirectory (as implied by your directive) then try the following instead:
# MultiViews must be disabled for the rewrite to work
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteRule ^read/([\w-]+)/([\w-]*)$ read.php?uid=$1&dvc=$2 [L,QSA]
\w is a shorthand character class that is the same as [0-9a-zA-Z_], to which we add the hyphen (-).
Since the URL /read maps directly to the file /read.php MultiViews must be disabled, otherwise mod_negotiation "rewrites" the request to /read.php (before mod_rewrite) without any URL parameters.
Note that I changed the quantifier from * to + in the first path segment, since this would seem to be mandatory in your URL?
Your example URL (/read/1234567/ABCDE) shows digits in the first path segment and letters in the second. If this is an accurate reflection of the type of URL then the regex should be made more restrictive to check just for this data type.
Aside:
RewriteEngine On # Turn on the rewriting engine
Note that Apache does not support line-end comments. This may be "OK" in this instance because the text after RewriteEngine On is simply ignored. In other cases you might get a 500 internal server error.

htaccess RewriteRule with hard-coded special characters

There are a lot of similar questions, but none of them seem to deal with hard-coded strings in the destination. I merely want to redirect any requests for a sub-domain to a Google Group, such as follows:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} gg.domain.com$
RewriteRule ^(.*)$ https://groups.google.com/forum/#!forum/myforum
I believe the problem is that the # character is being interpreted as a comment and thus the line is ignored. I have looked through the documentation for rewrite flags, and the only options I saw which might be relevant were [B] and [NE], neither of which seem to help, as I think they only work on string transformations.
The # character in your url should not be treated as a comment (it isn't for me). If it is treated as such though, you can escape it to let it loose it's special meaning. Escaping is the act of puting a \ before a character. Besides that, you require the [NE] flag to prevent the # from being ascii encoded.
RewriteCond %{HTTP_HOST} gg.domain.com$
RewriteRule ^(.*)$ https://groups.google.com/forum/\#!forum/myforum [NE]

symbols in .htaccess redirection issue

I'm having some issues with redirecting some pages with "%11" and "%28".
I'm trying to redirect a couple of pages, the rest work but I realized those with some symbols in it are not redirecting.
For example:
Redirect 301 /cars/mercedes%11benz/ http://www.example.com/cars/mercedes-benz/
Redirect 301 /alfa-romeo/alfa-romeo-147-%282001%E2%80%932009%29-2008090174/ http://www.example.com/cars/alfa-romeo-147/
do not work.
Thanks in advance for the help.
Basically specifies that only ASCII text is allowed. Might u having white spaces in url. Please remove them.
As for mod_rewrite, I believe that you've got that right except that the dot character in the character range need not be escaped, i.e., the hyphen is properly located at the beginning and the space is escaped. The ΓΌ probably doesn't need to be escaped but it shouldn't hurt). As for browsers making the conversion, that's a "browser thing" which Apache understands (and converts internally to the correct character).
Try this:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteRule ^. http://www.example.com/ [R=301,L]
RewriteRule ^/cars/mercedes-benz/ http://www.test-site.com/cars/mercedes%11benz/ [QSA]

allow Special Characters in URL

I want to allow special charecters in URL
my htaccess code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^blog/?$ SocialNetwork/blog.php [L]
RewriteRule ^blog/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_[]]+)/?$ SocialNetwork/blog.php?id=$1&title=$2 [L]
</IfModule>
it works for
blog/1
blog/1/hii
blog/1yooo_title
but does not work for
blog/1/[hii-this is [] title
In your last rewrite rule, try escaping the square brackets. Also if you need to match spaces you need to include \s in there:
^blog.php/(\d+)/([A-Za-z0-9-_\[\]\s]+)/?$
Or perhaps consider the simpler which accepts anything in the title:
^blog.php/(\d+)/(.+)/?$
Another point is that you should not have spaces in your URLs. They should be escaped to "+" or %20. So depending on this your regex would change, except the last one I proposed should work.
I'm pretty sure that you can't use spaces in a URL. Ever.
only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
http://www.ietf.org/rfc/rfc1738.txt

Resources