htaccess is changing unescaping my uri - apache

First off, I'm not sure that it's called escaping.
Basically I'm making a search page on my website. It works correctly without htaccess but once I add htaccess rewrite rules special characters are changed from escaped to regular characters.
Example.
Without htaccess
http://localhost:8888/search.php?s=hey%21%40%23%24
s variable returns hey!##$
With htaccess
RewriteRule ^search/(.*)$ search.php?s=$1 [NC,L]
http://localhost:8888/search/hey%21%40%23%24
s variable returns hey!# which tells me that # is commenting out the rest of the line.
How can I set it up so that it keeps special characters (!##$) escaped in my htaccess file?
I understand that the URI is read/converted before any rules applied, so how can I reconvert it? or is this not possible?
Basically I just want my rewrite rules to use this uri http://localhost:8888/search/hey%21%40%23%24and for s to return hey!##$

Use B flag to to escape non-alphanumeric characters before applying the transformation:
Options -MultiViews
RewriteEngine On
RewriteRule ^search/(.*)$ search.php?s=$1 [NC,L,B,QSA]

Related

htaccess URL rewrite page.php?page=string containing a hyphen

I need a solution for my redirect problem. In .htaccess file I have this code:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) category.php?page=$1 [NC,L,QSA]
And this is working for a query like
category.php?page=pinturas and redirect to category/pinturas.
But, if the string have - like this:
category.php?page=aquoso-madeira it redirects to category/aquoso
Any help?
category.php?page=aquoso-madeira It redirect to category/aquoso
Because your RewriteRule pattern does not include the hyphen (-), so it matches everything up to, but excluding, the first hyphen.
Include the hyphen in the character class:
RewriteRule ^category/([a-zA-Z0-9-]+) category.php?page=$1 [NC,L,QSA]
Note that the hyphen (-) must go at the start or end of the character class, since this is a special character and has alternative meaning when used elsewhere.
And this working for query like category.php?page=pinturas and redirect to category/pinturas
Note that this rewrite does the complete opposite of what you are describing.
This rule takes a request like category/pinturas and internally rewrites it to category.php?page=pinturas. (There is no external redirect here - which might be in another part of your code?)

If in htaccess for 301 Redirect special characters anywhere in URL to another characters?

Rule: Redirecting all URLs that contains %252C+ to ,- and redirecting + to - only if the + comes with %252C+.
e.g1: http://www.example.com/FortWorth%252C+TX/
to: http://www.example.com/FortWorth,-TX/
e.g2: http://www.example.com/Fort+Worth%252C+TX/
to: http://www.example.com/Fort-Worth,-TX/
e.g3: http://www.example.com/Fort+Worth/
to: http://www.example.com/Fort+Worth/
(Should not redirect this one)
Note: Please remember your code should not be only for the above URL but for all URLs with the above rule.
Thanks Sumurai8 , for your solution but it needs to be modified like This code :
RewriteEngine On
RewriteRule ^([^+]+)\+(.+)%2C+\+(.*)$ /$1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C+\+(.*)$ /$1,-$2 [R,L]
The first argument of RewriteRule is matched against the %-decoded url, so it should be matched against http://www.example.com/FortWorth%2C+TX/ for example instead of http://www.example.com/FortWorth%252C+TX/.
Assuming the strange substring only occurs once, and the "+" only appears once before that, you can do it with the following two rules. If multiple +'s can occur, things start to turn ugly quickly, because you'll need to either add a lot more rules, or start handling it recursively.
RewriteRule ^([^+]+)\+(.+)%2C\+(.*)$ $1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C\+(.*)$ $1,-$2 [R,L]
I am not sure if mod_rewrite will escape the , character as a "special character". It is a valid character to use in an url. If it gives you problems, you could use the [NE] flag to prevent escaping such characters.

Using mod_rewrite with chinese characters in Apache

I am having trouble finding information on Apache mod_rewriting using Chinese characters (all the info I can find relates to numbers).
I want to rewrite /character.php?character=宠 (where the character is the result of a search and thus will vary) to /character/宠.
This is my (poor) attempt:
RewriteRule ^character/?$ characters?character=$1 [NC,L]
I would appreciate any help.
Thanks,
Dan
First of all, your Regular Expression is incorrect. Using the ? tells mod_rewrite that the character before it is optional. It is not a placeholder for any character.
You should be doing this instead:
RewriteRule ^character/(%[A-Z0-9]{3})$ characters?character=$1 [NC,L]
This rule assumes you want to only capture one character. If this is not the case, or you need the same rule elsewhere, then swap out (%[A-Z0-9]{3}) for (%[A-Z0-9]+).
You also need to make sure that your .htaccess file is saved in Unicode format (UTF-8).
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^character/(.+)$ /characters?character=$1 [NE,NC,L,QSA]

Conditional rewrite in the regex, not with RewriteCond

I wrote a regex for a rewrite rule that includes a negative lookahead to replace the rewriteCond line, because WordPress only accepts two values: pattern -> substitution.
It should find findme.html _here, regardless of where it's requested to be:
mydomain.com/_here/findme.html
e.g.
(Sorry, I can't modify the swf which will request findme.html in the wrong places)
So, given findme.html could be requested to be in, e.g.:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
The rewrite should make them all
mydomain.com/_here/findme.html
So, I made a rewrite rule that Wordpress will accept me as follow
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
So it only matches URLs which doesn't contain "_here" in it, to prevent extra rewriting or a loop.
The problem is IT DOES loop.
What did I miss?
It looks to me like you want to move the .* that is before (?!_here) to after it because (?!_here) is a negative lookahead, so it check that the text _here does not come after it. What your regular expression is actually checking is whether your url starts with some character sequence that is not followed by _here, and _here is a character sequence not followed by _here. Then your rule becomes
RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]
Also, it looks like your pattern will exclude paths with subdirectories such as
mydomain.com/directory/subdirectory/findme.html
If you also want to include those, the pattern should be
RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]

.htaccess rewrite rule won't unicode characters

I am using the following ModRewrite to make my urls look cleaner:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?key=$1
It allows use of letters and numbers just fine, but it produces a 400 error when i try to use %, which I require to use unicode characters for # / ', etc.
Any reason behind this? Thanks.
you should use B flag in your rewrite rule. take a look at apache manual .
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-#$%^&]+)/?$ index.php?key=$1 [B]
Edit:
mod_rewrite uses unescaped characters, so if you want to use unicode characters, use them in rewrite rule and save .htaccess file in unicode!