Lowercase cyrillic {QUERY_STRING} - .htaccess

On my site i have links like:
site.com/search.php?str=СТРОКА
site.com/search.php?str=Строка
I want to make them like:
site.com/search.php?str=строка
i.e. I want to lowercase {QUERY_STRING}
How I can do this? I can't use RewriteMap!
Thanks in advance!

If you have links with ciryllic query_string like this:
site.com/search.php?str=СТРОКА
site.com/search.php?str=Строка
And you want to make them like e.g. lowercase it
site.com/search.php?str=строка
And you can't use RewriteMap, follow these steps:
1) Add to .htaccess those rows:
RewriteCond %{QUERY_STRING} %D0%([AF].)
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L]
%D0%([9A].)|(81) - detecting [А-Я]
2) Place rewrite-strtolower.php nearly of .htacces
<?php header('Content-Type: text/html; charset=utf-8');
function myUrlEncode($string) {
$entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
$replacements = array('!', '*', "'", "(", ")", ";", ":", "#", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
return str_replace($entities, $replacements, urlencode($string));
}
if(isset($_GET['rewrite-strtolower-url'])) {
$url = $_GET['rewrite-strtolower-url'];
unset($_GET['rewrite-strtolower-url']);
$params = http_build_query($_GET);
if(strlen($params)) {
$params = '?' . myUrlEncode(mb_strtolower(urldecode($params),"UTF-8"));
}
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . $url . $params, true, 301);
exit;
}
header("HTTP/1.0 404 Not Found");
die('Unable to convert the URL to lowercase. You must supply a URL to work upon.');
?>
3) Profit

Related

Prolog: slash separated string to list

I have a string like '/home/user/something/' and I want to convert it to ['/', 'home', '/', 'user', '/', 'something', '/']
How can I do this?
1- s predicate takes the input and gives the final split answer.
2- split_string is used to return just the words, so it returns L = ["home", "user", "something"]. Notice we don't have the /.
3- Now we need to add the "/". We use addslash predicate, before every word we add a "/". For the last "/" it checks the base predicate and the last "/" is added to the list.
s(X,L1):-
split_string(X, "/", "/", L),
addslash(L,L1).
addslash([],["/"]).
addslash([H|T],[H1,H|L]):-
H1="/",
addslash(T,L).
?- s('/home/user/something/', L).
L = ["/", "home", "/", "user", "/", "something", "/"]

htaccess RewriteRule URL's

I use htaccess to rewrite my URL's so here is what I have:
RewriteRule ^([^-]+),([a-z0-9._.-]+),([^-]+) index.php?go=$1&lang=$2 [L]
and code PHP:
function rewrite_seo($id, $title, $langs) {
global $core;
if($core->getSettings('seof')) {
$result = $id.','.$langs.','.changeSigns($title).'';
} else $result = '?go='.$id.'&lang='.$langs;
return $result;
}
My URLs look like:
http://localhost/script/12,en,home
but want to look like this:
http://localhost/script/en/home,12
how to modify this code?
localhost/script/12,en,home TO
localhost/script/en/home,12
Did u try to modify line 4?
Put that:
$result = $langs.'/'.changeSigns($title).','.$id;
Please take a look to PHP.net webpage http://php.net/manual/de/internals2.opcodes.concat.php
You have to change the links written in PHP as pointed out in the previous answer :
$result = $langs.'/'.changeSigns($title).','.$id;
And then change the matching rule in your htaccess :
RewriteRule ^([a-z0-9._.-]+)\/([^-]+),([^-]+) index.php?go=$2&lang=$1 [L]
Even better, you can make more specific capture regex, so it would be easier to read and understand :
RewriteRule ^([a-z]+)\/([0-9]+),([^-]+) index.php?go=$2&lang=$1 [L]

Rewrite rule with redirect in php

I seem to have problem with an .htaccess rewriterule.
What I want to do is a dispatcher of this form:
[website]/[parameter1-text]/[parameter2-text]
I have in my .htaccess the following:
RewriteRule ^(.*)/(.*)$ dispatcher.php?s1=$1&s2=$2
So far so good. But s1 and s2 are some variables that need to be matched in a database (mysql) and return the true variables that feed the script view.php in the following format:
view.php?category=[s1]&subcategory=[s2]&objects=5
How can I redirect this properly using header("Location: /view.php[...]"); and having the url unchanged (for example: http://example.com/CARS/BLUE http://example.com/SLIPPERS/RED/ )
First of all fix some minor issues with your .htaccess:
RewriteRule ^([^/]+)/([^/]*)/?$ dispatcher.php?s1=$1&s2=$2 [L,QSA]
Now inside dispatcher.php do your Mysql stuff and fetch values of s1 and s2 from database. Once you're done and want to internally forward your request to: view.php?category=[s1]&subcategory=[s2]&objects=5 have this type of PHP code:
$_GET['category'] = $value-of-s1-from-DB;
$_REQUEST['category'] = $_GET['category'];
$_GET['subcategory'] = $value-of-s2-from-DB;
$_REQUEST['subcategory'] = $_GET['subcategory'];
$_GET['objects'] = '5';
$_REQUEST['objects'] = $_GET['objects'];
// now include view.php
// all your parameters are available in view.php in $_GET array
require_once("view.php");

How can I go back to showing the content of /index.php?

Here's part of my .htaccess file:
.htaccess:
RewriteRule ^([a-z0-9A-Z_-]+)$ /article/index.php?title=$1 [L]
But the problem is that when title is empty it doesn't show me the content of http://www.domain.com/index.php. I can't even link to http://www.domain.com/ anymore without throwing the error.
PHP: http://www.domain.com/article/index.php
<?php
if(!empty($_GET['title']))
{
$a = mysql_query("SELECT * FROM `articles` WHERE `title` = '".mysql_real_escape_string($_GET['title'])."'");
}
else
{
$a = mysql_query("SELECT * FROM `articles` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."'");
}
$b = mysql_fetch_assoc($a) or mysql_error();
?>
You pretty much have 2 choices:
1. Use this rule: If home page is hit, then title parameter will be empty (so you have to handle this moment somehow -- adjust your PHP code accordingly).
RewriteRule ^([a-z0-9A-Z_-]*)$ /article/index.php?title=$1 [QSA,L]
2. Use specific rule for home page/website root hit (add it before your current rule):
RewriteRule ^$ /article/index.php?title=home [QSA,L]
The title=home means it's a hit for website root (you can change home to whatever you want).

How do I get next parameter in my .htaccess

Current URL
http://domain.com/page/2/
.htaccess
RewriteRule ^page/([0-9]+)/$ /index.php?p=$1 [L]
Next URL
http://domain.com/page/2/?by=1
How to get the by=1?
Because when I print_r($_GET) It's only display Array ( [p] => 2 )
What should I write in my .htacces to get the [by] => 1
Change the end of your line from [L] to [QSA,L] to preserve the query string.

Resources