allow Special Characters in URL - .htaccess

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

Related

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]

multiple url rewriting mod

I have two pages say
http://assistque.com/services.php?prod_id=35&prod_url=Operating%20System%20–%20Windows
and
http://assistque.com/sub_services.php?sp_id=13&sp_url=Technical%20Support%20For%20KASPERSKY
i wrote two rules for two different pages but the for the second page it uses first rule .
Please help
RewriteRule ^([^/]*)/([^/]*)\.html$ /services.php?prod_id=$1&prod_url=$2
RewriteRule ^([^/]*)/([^/]*)\.html$ /sub_services.php?sp_id=$1&sp_url=$2
This is because the patterns in the both rules are the same, try changing them into something unique:
RewriteRule ^services/([^/]*)/([^/]*)\.html$ /services.php?prod_id=$1&prod_url=$2 [L]
RewriteRule ^subservice/([^/]*)/([^/]*)\.html$ /sub_services.php?sp_id=$1&sp_url=$2 [L]
Then in your links you add something like:
operating systems windows
Note, that you should not use spaces inside an url, so in this case I assume those will be replaced with an - character.
It may also be better to narrow down allowed characters and numbers in your rewrite rule:
# will match services/<number>/<string>.html
# the NC flag states that the rule is case insensitive
RewriteRule services/([0-9]+)/([a-z-]+)\.html$ /services.php?prod_id=$1&prod_url=$2 [NC,L]

htaccess read whitespaces "was not found on this server"

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]

Mod_rewrite condition that will accept + in string

So first, I promise I've looked everywhere but I'm either not finding the right answer or not knowing I found that right answer and moved on (possible). I have some strings that might contain one or more words that are spaced with a + inside my normal page which would look like this http://beaneandco.com/pageTESTING.php?name=Boys+Shirt. This page works fine except it's dirty. I wrote a rewrite code that cleans this up.
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9])$ pageTESTING.php?name=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9])/$ pageTESTING.php?name=$1 [QSA,L]
If my page is just one word, as in Boys, it works just fine when shortened. For whatever reason though, when it has two words or more seperated by a + it no longer works. There is a GET at the beginning of my page that has this string if that changes anything. So.........what in the world am I missing?
Thanks in advance.
Add in the regexp code the +, space and other characters you have in URL.
Try this in your htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_\+ \-])$ pageTESTING.php?name=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\+ \-])/$ pageTESTING.php?name=$1 [QSA,L]

Do you have to escape a forward slash when using mod_rewrite?

With regards to the forward slash "/" when giving a regex to RewriteRule or RewriteCond, or anything else related to .htaccess in particular, is there a need to escape the forward slash?
Here is an example of what I am trying to achieve
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region\/|regions\/)$ regions [R=301,L]
RewriteRule ^(county|county\/|counties\/)$ counties [R=301,L]
RewriteRule ^(city|city\/|cities\/)$ cities [R=301,L]
The above works fine, and it continues to work fine when I remove the backslashes as shown below
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region/|regions/)$ regions [R=301,L]
RewriteRule ^(county|county/|counties/)$ counties [R=301,L]
RewriteRule ^(city|city/|cities/)$ cities [R=301,L]
Which one is the correct way? Are they both wrong?
Is there any special reason the forward slash should be escaped, or shouldn't?
My guess is that the forward slash does not need to be escaped because it isn't a special character, as far as I know. But I just want to be sure.
In case you're wondering the point of this code, it redirects city, county, and region (with or without a forward slash) to their plural equivalents. Furthermore if the plural has a forward slash it removes the forward slash.
No, you do not have to escape slashes. Forward slashes don't have any special meaning in regular expressions.
The one common character that has bitten me in the past is ? in query strings. That one you do have to escape.

Resources