Rewrite rule accepting # - regex

I have the current RewriteRule:
RewriteRule ^search/([0-9a-zA-Z-\s-:]+)(/?)$ search.php?search=$1 [NC,L,NE]
I'm wanting to have an url like this /search/#ff2266/, to accept hex (with the #) colors in the url and then have this value in the $_GET['search'] variable. But not only this, but I'm having problem with the #.
Is there any way i could do something like this: ([#0-9a-zA-Z-\s-:]+) ?
I've tried the rule:
RewriteRule ^search/(.*)(/?)$ search.php?search=$1 [NC,L,NE]
And then in the search.php the following code:
echo "var_dump($_GET): ";
var_dump($_GET);
and with the url /search/#f44336/ the result is:
var_dump(Array): array(0) { }

See, the part of a URI after the # is called "fragment" and is by definition only available/processed on client side (see https://en.wikipedia.org/wiki/Fragment_identifier).
On the client side, this can be accessed using javaScript with window.location.hash.
If you want to access that number in your search file, you could pass it to there without the hash char and use it that way.

Related

WordPress - Rewrite rules multiple para

i have an URL like this:
http://domain.de/projekte/agbz/userprofile/?page=member&memberID=Z4911331&land=deutschland&fa=zahnarzt&vorname=Henryk&nachname=Donnerstag
I want to rewrite this URL in a structure like this:
https://www.domain.de/deutschland/zahnarzt/dr_michael_claar_kassel_Z4901196/
my rewrite Rule in my functions.php is like that, but its not working:
add_rewrite_rule(
'/agbz\/(\w*)\/\?page=(\w*)&memberID=([a-z]{1}\d*)&land=(\w*)&fa=(\w*)&vorname=(\w*)&nachname=(\w*)/',
'index.php?page_id=$matches[1]&page=member&memberID=$matches[3]&land=$matches[4]&fa=$matches[5]&vorname=$matches[6]&nachname=$matches[7]',
'top');
Can anybody help me at this point?
For example
www.example.com/somePage/products/?mfg=UETD342
to
www.example.com/somePage/products/UETD342/
you can add custom URL tag using below code:
function custom_rewrite_tag() {
add_rewrite_tag('%mfg%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
and add below code to add your own custom rewrite rule for loading your page with this parameter:
function custom_rewrite_rule() {
add_rewrite_rule('^products/([^/]*)/?','index.php?page_id=your_page_id&mfg=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
you need to add both codes in your theme's functions.php
For a more detailed knowledge of url rewriting visit: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

fastcgi_cache_key exclude some args in request uri

My fastcgi_cache_key is:
fastcgi_cache_key "$host$request_method$request_uri";
My $request_uri has timestamp and signature in it:
/abc/xyz?product_id=10529125896&shop_id=17224077&shop=abc.com&path_prefix=%2Fa%2Fcomment&timestamp=1503044416&signature=882102c51c7b7bd4c5d8521a6565fc70c27b908547316f1123eb4af13b19f2da
So, the cache always MISS (because it has different timestamp and signature). My question is:
I want to create new var and use that var for fastcgi_cache_key. That var will has something like this:
myvar
/abc/xyz?product_id=10529125896&shop_id=17224077&shop=abc.com
fastcgi_cache_key will like this:
fastcgi_cache_key "$host$request_method$myvar";
How can I do that ? Thanks so much.
There are two ways to do it.
if ($request_uri ~ "([^\?]*)\?(.*)timestamp=([^&]*)&?(.*)") {
set $args $2$4;
}
fastcgi_cache_key "$host$request_method$args";
This will remove the timestamp. You can either modify the pattern to ignore one more field or you can use it twice to remove the field from $args.
Next option is to use openresty or Nginx with lua which allows you to execute Lua script in your code. if conditions are not considered good. But then having lua increases your software requirement

UrlRewrite, removing part of unmatched pattern

I am kinda stumped here and could use some help. Transfering a bunch of pages from 1 server to another, and changing the path a bit to include the new user ID. I have the map completed of old urls to new ones, but my rewrite just isn't working.
Here is my current attempt at the htaccess file:
RewriteRule /users/MikeJones[^.*] http://new.com/profiles/1234/mikejones/ [NC,R=301,L]
I would expect that any of these URL's, should all goto the URL I gave, not append part of the original URL on it.
My results are as follows:
old.com/users/MikeJones > new.com/profiles/1234/mikejones/ (Good!)
old.com/users/MikeJones/ > new.com/profiles/1234/mikejones// (Bad)
old.com/users/MikeJones/test > new.com/profiles/1234/mikejones//test (Bad)
old.com/users/MikeJones1234 > new.com/profiles/1234/mikejones/1234 (Bad)
old.com/users/MikeJones?test=that > new.com/profiles/1234/mikejones/?test=that (Bad)
I would like to always go to the same url, no matter what else was on the original url.
Like this:
old.com/users/MikeJones > new.com/profiles/1234/mikejones/
old.com/users/MikeJones/ > new.com/profiles/1234/mikejones/
old.com/users/MikeJones/test > new.com/profiles/1234/mikejones/
old.com/users/MikeJones1234 > new.com/profiles/1234/mikejones/
old.com/users/MikeJones?test=that > new.com/profiles/1234/mikejones/
Any suggestions?
Not sure how your rule is working at all in your htaccess file because mod_rewrite strips off the leading / from the URI, so you'd need to match against users/ and not /users/:
RewriteRule ^users/MikeJones http://new.com/profiles/1234/mikejones/? [L,R=301,NC]
should be all you need. You don't need any other rules, and if you do have other rules, make sure this one is before them.

htaccess 404 error conditional code for 301 redirect

I have paginated URLs that look like http://www.domain.com/tag/apple/page/1/
If a URL such as http://www.domain.com/tag/apple/page/*2/ doesn't exist, or page/2 doesn't exist, I need code to redirect this to a page such as http://www.domain.com/tag/apple/ which would be the main tag page.
I've currently have the following code:
RewriteCond %{HTTP_HOST} !^http://www.domain.com/tag/([0-9a-zA-Z]*)/page/([0-9]*)/$
RewriteRule (.*) http://www.domain.com/tag/$1/ [R=301,L]
In this code, if the URL does not exist it redirects to the main tag page, but its not working.
Does anybody have an hints or solutions on how to solve this problem?
If I understand what you're saying, you are saying that you have a list of rewritten URLs (using mod_rewrite); some of them exist, some of them don't. With the ones that don't exist, you want them to be redirected to a new page location?
The short-answer is, you can't do that within htaccess. When you're working with mod_rewrite, your rewritten page names are passed to a controller file that translates the rewritten URL to what page/content it's supposed to display.
I'm only assuming you're using PHP, and if so, most PHP frameworks (CakePHP, Drupal, LithiumPHP, etc.) can take care of this issue for you and handle custom redirects for non-existing files. If you have a custom-written application, you'll need to handle the redirect within the PHP website and not in the .htaccess file.
A very simple example of this would be:
<?php
function getTag($url) {
if (preg_match('|/tag/([0-9a-zA-Z]*)/|', $url, $match)) {
return $match[1];
}
return '';
}
function validateUrl($url) {
if (preg_match('|/tag/([0-9a-zA-Z]*)/page/([0-9]*)/|', $url, $match)) {
$tag = $match[1];
$page = $match[2];
$isValid = // your code that checks if it's a URL/page that exists
return $isValid;
}
return false;
}
if (!validateUrl($_SERVER['REQUEST_URI'])) {
$tag = getTag($_SERVER['REQUEST_URI']);
header ('HTTP/1.1 301 Moved Permanently');
header('Location /tag/' . $tag . '/');
die();
}
?>

Can mod_rewrite convert any number of parameters with any names?

I'm a total n00b at mod_rewrite and what I'm trying to do sounds simple:
instead of having domain.com/script.php?a=1&b=2&c=3 I would like to have:
domain.com/script|a:1;b:2;c:3
The problem is that my script takes a large number of parameters in a variety of combinations, and order is unimportant, so coding each one in the expression and expecting a certain order is unfeasible. So can a rule be set up that simply passes all of the parameters to the script, regardless of order or how many parameters? So that if someone types
domain.com/script|a:1;b:2;j:7 it will pass all those params and values just the same as it would with domain.com/script|b:2;a:1; ?
Thanks!
I would use PHP to parse the requested URL path:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$params = array();
foreach (explode(',', substr($_SERVER['REQUEST_URI_PATH'], 6)) as $param) {
if (preg_match('/^([^:]+):?(.*)$/', $param, $match)) {
$param[rawurldecode($match[1])] = rawurldecode($match[2]);
}
}
var_dump($params);
And the mod_rewrite rule to rewrite such requests to your /script.php file:
RewriteRule ^script\|.+ script.php [L]