.htaccess rewrite rule won't unicode characters - apache

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!

Related

APACHE Mod Rewrite - Dynamic URLs to Semantic URLs

I have a little web app based on Google Maps. I have URLs like this:
http://www.example.com/web.php?u=olives-restaurante-de-ensaladas-en-margarita
and I want to have exactly this:
http://www.example.com/web/olives-restaurante-de-ensaladas-en-margarita
So I want to convert my parametric dynamic URLs into semantic URLs
How can I achieve this?!
I have tried to put the following code on my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^web/([A-Z0-9]+)/$ web.php?u=$1
But sadly, when I try to access http://www.example.com/web/olives-restaurante-de-ensaladas-en-margarita it redirects me to 404 page.
What I'm doing wrong?
RewriteRule ^web/([A-Z0-9]+)/$ web.php?u=$1
http://www.ubikate.com.ve/web/olives-restaurante-de-ensaladas-en-margarita
Your RewriteRule pattern (^web/([A-Z0-9]+)/$) does not allow hyphens, enforces a trailing slash and only matches uppercase letters, so this won't match the URL you are requesting.
Try something like the following instead:
RewriteRule ^web/([\w-]+)$ web.php?u=$1 [L]
The \w is a shorthand character class that is equivalent to [a-zA-Z0-9_].

301 Redirects Rewrite Rule to replace underscore with hyphen and remove subdirectory

I need help creating a rewrite rule/301 redirect for the following link structures in the .httaccess folder.
Source URL: www.example.com/sub_directory/product_name_1.html
Destination URL: www.example.com/prodcut-name-1.html
The requirements for the redirect then are as follows:
Remove /sub_directory/
Change all underscores '_' to hyphens '-'
Unfortunately my regex isn't very good. I've tried searching around and but the solutions from other post with similar issues where not working for me (such as here)
Any help on a solution for this would be much appreciated. Also if you could please explain the why/how of it. I'd like to be able to better understand this.
Answer from #Walf is close but requires some changes e.g. regex anchors and DPI flag.
You can use these rules on top of your site root .htaccess:
RewriteEngine On
# remove /sub_directory/ when there is no _ left
RewriteRule ^sub_directory/([^_]+)$ /$1 [R=301,NC,NE,L]
# use recursion based rule to replace _ by -
RewriteRule ^(sub_directory/[^_]*)_+(.*)$ $1-$2 [NC,N,DPI]
# rest of your rules go here
Something like this
RewriteEngine on
RewriteBase /
RewriteRule ^(sub_directory/[^_]*)_+(.*) $1-$2 [DPI,N]
RewriteRule ^sub_directory(?:$|/(.*)) http://%{HTTP_HOST}/$1 [R=301,L]
It first loops through URLs that are in that subdirectory, to replace consecutive underscores with a single hyphen. It's done first so it doesn't interfere with other URLs that may contain an underscore. It then externally redirects (the cleaned) requests for that subdirectory to the root. The ugly grouping makes sure it only applies to exactly that folder.

htaccess is changing unescaping my uri

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]

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]

Apache URL Rewrite to domain.com/custom_url_name

Using Apache on a Red Hat server, I'm trying to rewrite the URL of a member's store on our website from:
domain.com/store.php?url=12345
to:
domain.com/12345
Using these rules, I can get it to work if I always remember to add an ending slash:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com/12345/ works,
but domain.com/12345 does not work.
Removing the slash in the last line of Rewrite code breaks a lot of stuff. Is there a way to get this to work both with or without that ending slash?
What if you made the slash optional? Furthermore, you probably to to specify something more specific than (.*), because domain.com/a/b/c/d/e will match. Instead, you can use a negated character class to specify everything other than a slash.
RewriteRule ^([^/]*)/?$ store.php?url=$1
Alternately, if you only want to capture numbers, you can use the \d shorthand class (which matches any digit) along with a + which specifies that at least one digit must be present:
RewriteRule ^(\d+)/?$ store.php?url=$1
Your attempt using ^(.*)$ fails because that would match any URL path. Use a more specific pattern than .*, maybe \d+ to allow only one or more digits:
RewriteRule ^(\d+)$ store.php?url=$1