Using mod_rewrite with chinese characters in Apache - 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]

Related

How do you change part of a URL from upper case to lower case?

Can't seem to get this right for my .htaccess file.
Want to change the spelling in a part of a URL. In the middle of the URL it is /Johannsen/ and I want to change it to /johannsen/ e.g. lower case.
If you have control over your vhosts through vhosts.conf or vhosts file, you can use RewriteMap.
in vhosts.conf, we activate the int: Internal Function, defining a RewriteMap named maplower, as we cannot use RewriteMap directive in .htaccess file:
#
RewriteMap maplower int:tolower
#
Restart apache2, under debian OS, we do it with
/etc/init.d/apache2 restart
Then in you directory/.htaccess, we use the previously defined RewriteMap as this:
RewriteEngine On
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . "${maplower:%{REQUEST_URI}}" [R]
It does this: with RewriteCond, we look at the %{REQUEST_URI} if there is any A-Z uppercase letter. If yes, redirect to an all lowercase url.
For example,
https://www.example.com/test/Johannsen =>
https://www.example.com/test/johannsen
https://www.example.com/test/NewDAte.php =>
https://www.example.com/test/newdate.php
This would seem to be a straightforward string replacement. The fact that you happen to be changing the first letter of the word to lowercase is irrelevant since this would seem to be the only word you are changing.
You can do this using mod_rewrite near the top of your .htaccess file:
RewriteEngine On
# Convert/redirect "/Johannsen/" to "/johannsen"
RewriteRule ^(.*/)J(ohannsen/.*) /$1j$2 [R=302,L]
This will redirect a URL of the form /foo/Johannsen/bar to /foo/johannsen/bar. The $1 and $2 backreferences capture the part of the URL-path before and after the J respectively.
If this is intended to be a permanent redirect then change the 302 (temporary) to 301 (permanent) once you have confirmed that it works OK.

Apache rewrite URLs to lowercase if url contains catalogsearch/result/?q=SOMETHING using .htaccess

redirect htaccess uppercase to lowercase after
http://example.com/catalogsearch/result/?q=
From: http://example.com/catalogsearch/result/?q=RASPBERRY
To: http://example.com/catalogsearch/result/?q=raspberry
That seems excessively heavy to add to .htaccess. While it's so easy and much more efficient to add it (for example in PHP) on the relevant page.
In PHP (at the top of your page):
$_GET['q'] = strtolower($_GET['q']);
You first need to add in the virtualhost configuration this:
RewriteMap tolower int:tolower
Then, in your .htaccess add this line:
** EDITED accordng to #croises corrections **
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${tolower:%{REQUEST_URI}} [R=301,L]
note: you need to restart apache to make it work
note2: it will lowercase the whole URL not only that part

.htaccess rewrite rule ignoring string, multiple GET variables in url

I am trying to make .htaccess rewrite rule to map 4 different get variables and exclude one string. String is unchangeable ie. always will remain same.
Current url is:
/car.php?make=bmw&model=z4&year=2006&color=black_metallic
It should be like this:
car/bmw-z4-2006-black_metallic-for-sale
This is I've done so far
RewriteRule ^car/^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4
Now I need to ignore string -for-sale at the end of the pretty url.
Any help greatly appreciated!
Try this
RewriteRule ^car/([^/-]+)-([^/-]+)-([^/-]+)-([^/-]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4 [QSA,NC,L]
Your delimiter is hyphen not forward slash hence your RewriteRule should also handle that accordingly:
Options -MultiViews
RewriteEngine On
RewriteRule ^car/^([^-]+)-([^-]+)-([^-]+)-([^-]+) car.php?make=$1&model=$2&year=$3&color=$4 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

.htaccess "?" Sign in file names trouble

There are static html files called blahblah.html?somestring. I'm always get 404 error.
When I try blahblah.html%3fsomething - it semi-works (shows html source as text), but ` need URLs to be exactly with question sign.
How to make Apache show to this files with exact URL containing "?".
I tried to rename file to "blahblah.html-somestring" + rule in .htaccess
RewriteRule ^/(.*).html\?(.*)$ /$1.html-$2 [NE]
it doesn't help
THIS HELPED ME:
I renamed all files to blahblah.html-somestring.html + added this to .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^[^.]+\.html/?$ %{REQUEST_URI}-%{QUERY_STRING}.html? [L,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^[^.]+\.html/?$ %{REQUEST_URI}-%{QUERY_STRING}? [L,NC,R]
This will rewrite blahblah.html?somestring to blahblah.html-somestring. However how the content will be served from blahblah.html-somestring, you will have to figure out.
Actually I can see two problems here:
1) You want to use the ? character as part of file names
I cannot see a plain solution of this problem. The question mark is part of the HTTP protocol and you can rewrite URLs but the desired solution is not possible with these tools. If you can live with the %3f approach this will be the most simple way to solve this.
2) You want to set correct MIME types beside from file suffixes
Apache detects the correct MIME type (e.g. text/html) depending on a files name. If the file is ending to .htm or .html Apache delivers the content with the MIME type text/html.
As your file names do not end with the expected suffixes and regular expression do work for the MIME table the only way to make this working is some regex within the mod_rewrite module. Here you can detect the suffix in the middle of the filename and are able to set the MIME type:
RewriteEngine On
RewriteRule ^(.+\.html.*)$ $1 [T=text/html]
Overall solution:
You can solve #1 and #2 with a simple Web application, checking the origin request string and delivering the matching file (as well as setting the correct MIME type). You can do such things for example with PHP.

.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!