Liferay friendly url with optional param - liferay

My URL have some params, so i use friendly URLs to solve that ugly URL liferays generates by default.
couple of this params sometimes are empty, that mean its no needed, but other times give me some ID i use for a query.
Example param not null:
https://myliferay.com/example-portlet/-/example-portlet/search/idTable-7
//it works
Example null param:
https://myliferay.com/example-portlet/-/example-portlet/search/idTable-null
//it works
With null param it works, but i dont like to see a null on my URL.
I want something like "":
https://myliferay.com/example-portlet/-/example-portlet/search/idTable-
//Doesnt work
But it doesnt work, its like the URL doesnt match the friendly URL pattern when a param its empty.
<route>
<pattern>/search/idTable-{idTable}</pattern>
<generated-parameter name="idTable">{idTable}</generated-parameter>
<implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
<!--more implicit params-->
</route>
How can i specify optionality of a parameter?

Its like all vars of friendly url use a regex. You can change/override this regex like this: {idTable:\d}
<route>
<pattern>/search/idTable-{idTable:.*}</pattern>
<generated-parameter name="idTable">{idTable}</generated-parameter>
<implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
<!--more implicit params-->
</route>
i used .* as regex for 0 or more characters, but i dont know if it will bring me problems later. If anyone knows why is not a good idea to use that regex comment it please.
Upgraded the regex with \d* to search only digits or empty: {idTable:\d*}
Info: Making URLs friendlier 7.0

Related

Yii2 Trailing Slashes in URL is breaking the route

I configured UrlManager in a project and It was working the way I wanted.
Now i tried to add a content whose name contains a trailing slash but i get an error 404 (Object not found).
For example:
www.test.com/article/detail/id_of_article/title_of_article
title_of_article = People are ... => Work
title_of_article = 1/3 of People are ... => Doesn't Work (Object not Found)
The Trailing Slash is breaking the Url although it is encoded in %2F
This is how i create url:
Html::a(Html::encode($model->title),
['article/detail', 'id' => $model->id, 'title' => $model->title])
I don't really know how I can deal with that.
For This , the best solution is to use slug names.
Instead of id and title, take one more field called slug_name in your database.
On Add or update of any record generate slug name and store in db.
For generating slug name, you can use custom function as below.
public function getSlugName($id,$title)
{
$slug=$id;
if(isset($title) && $title!=null)
{
// remove all spacea
$slug.='-'.str_replace(' ','-',strtolower($title));
}
$slug=preg_replace('/[^A-Za-z0-9\-]/', '', $slug); // Removes special chars.
$slug=str_replace(array('--'), '-', $slug); // remove multiple --
return $slug;
}
This function will return you uniq name. So you can use it in url.
This is also help in SEO.
Could be you need URL normalization
Since version 2.0.10 UrlManager can be configured to use UrlNormalizer
for dealing with variations of the same URL, for example with and
without a trailing slash.
NB by default UrlManager::$normalizer is disabled. You need to explicitly configure it in order to enable URL normalization.
see how here http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#url-normalization
There is an encodeParams property of UrlRule. Please try with that.

How to use add_rewrite_rule function, while permalink structure is disabled?

I am using the add_rewrite_rule() function to modify my URL structure.
I'm wanting to use add_rewrite_rule to add a custom rule but these rules only get added in when other than default settings are selected in my permalink settings area.
i.e. in the settings there are following options:
- Default http://localhost/wordpress/?p=123
- Day and name http://localhost/wordpress/2014/08/14/sample-post/
- Month and name http://localhost/wordpress/2014/08/sample-post/
- Numeric http://localhost/wordpress/archives/123
- Post name http://localhost/wordpress/sample-post/
- Custom Structure http://localhost/wordpress
So, when I select other then 'Default', my add_rewrite_rule() function works, but while selecting 'Default', the function doesn't seem to be work. So please suggest me how to work the function in any condition. Any help would be Appriciated.
Update:
I think the problem lies here:
When I use this, while selecting 'Default':
get_option('permalink_structure');
I got nothing.
While in the other cases, there are some values like:
/%postname%/
/archives/%post_id%
/%year%/%monthnum%/%postname%/
The Default permalinks, or so called "Ugly" permalinks, are not adding anything to the .htaccess file, so the Apache rewrite engine is not enabled. Without the rewrite engine, no rewrites can be done. So the short answer is that rewrites are not possible with Default permalinks.
I can recommend you to use rewrites along with query vars. When adding a rewrite rule, pass your custom data to a query var, and build the functionality around that query var. This way your functionality will work in all situations and with all permalink types.
So for example if you have the following rule:
add_rewrite_rule('^sometest/([^/]*)/?','index.php?custom_query_var=$matches[1]', 'top');
and you have the custom_query_var added as a query var by using the following code:
function add_query_vars_filter( $vars ){
$vars[] = "custom_query_var";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
then when Default permalinks are selected, the following URL would work for you:
http://yoursite.com/index.php?custom_query_var=abc
and if "Pretty" permalinks are selected, the URL rewriting would work and your URL would look the following way:
http://yoursite.com/sometest/abc/
which is basically the best that can be achieved with rewrites.
I agree with #Martin. Here's a resource that will help https://core.trac.wordpress.org/ticket/15235
use this:
function my_add_query_vars( $qvars ) {
$qvars[] = 'business-coaching';
$qvars[] = 'country';
$qvars[] = 'territory';
$qvars[] = 'region';
return $qvars;
}
add_action('query_vars', 'my_add_query_vars');
//Write the rule
function add_analytic_rewrite_rule()
{
// Regex:The regex to match the incoming URL is:business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?
// Redirect Rule :The resulting internal URL: `index.php` because we still use WordPress
// `pagename` or page_id=45 because we use this WordPress page
// `country` : we will assign the first captured regex part to this variable
// `territory` we will assign the second captured regex part to this variable
// `region` we will assign the third captured regex part to this variable
add_rewrite_rule('business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?','index.php?page_id=45&country=$matches[2]&territory=$matches[`enter code `enter code here`here`4]&region=$matches[6]','top');//superfinal
}
add_action('init', 'add_analytic_rewrite_rule');

How to split string from pattern?

How to put checks on pattern in Express?
I have a url:
http://localhost:3030/total?id=1234#12
I get the complete url by req.url which contains:
/total?id=1234#12
Is there any string pattern match syntax in Express, like I would like to separate the /total?id= part.
It looks like you're looking for req.query. That will give you an object based on the keys and values in your query string.
You can use both req.path and req.query to separate /total?id=1234#12.
req.path will give you "/total"
req.query.id will give you "1234#12"

url rewriting : How to rewrite 2 urls with same number of parameters?

I've created some url rewriting rules.
It works fine.
But i have 2 pages with urls like that :
actualites.php?id=xxx& alias=xxx
--> Result : id-alias.php (ex : 1-article_name.php)
equipe.php?id=xxx& alias=xxx
--> Result : id-alias.php ((ex : 1-article_name.php)
These 2 pages are different, not the same design, etc.
I would like to rewrite these 2 different pages. My question is how to have two different rules for these 2 urls.
Since they have 2 parameters, i have only one rule applied (the first one)
#become : 1-alias.php
RewriteRule ^([0-9]*)-(.*)\.php$ actualites.php?cat=$1&alias=$2 [L]
#become : 1-alias.php
RewriteRule ^([0-9]*)-(.*)\.php$ equipe.php?id_equipe=$1&alias=$2 [L]
Unless you want to add code in actualites.php that first checks whether the parameter values for "cat" and "alias" are valid, then you can't.
If you want to code that into actualites.php, check if the parameters are valid, and if not, send the params to equipe.php as "id_equipe" and "alias". Otherwise you'll need to add something to the URL to validate which request you're actually mapping to. For example:
http://yourdomain.com/a/1-article.php --> /actualites.php?cat=1&alias=article
http://yourdomain.com/e/1-article.php --> /equipe.php?id_equipe=1&alias=article
Without that /a/ or /e/, there's no way to tell what to map to. If you don't use something like that, you'll need to do all validation in your php.

how to rewrite search[] to something else with htaccess

I have created a search form with get method. But when the url looks like this search.php?search[] or search?search[] (mod_rewrite) then I get a sql fattal error. It's passing an array and I want to avoid that problem.
my question is how do I redirect a person from that url to search.php
It sounds like you are directly passing the ?search[] query string variable into your SQL. mod_rewrite won't fix this for you... what if I decide to call your page with http://www.yoursite.com/search.php?search=;DROP TABLE users;? You simply aren't able to use mod_rewrite to predict all the bad kinds of input that a user can come up with.
Your code needs to be doing input validation and sanitization. You must assume that everything your script receives from the user is malicious and dangerous. That includes all data inside $_GET, $_POST and $_COOKIE.
The right solution here is to check that $_GET['search'] is a valid value to be passing to your SQL. Something like:
if (is_string($_GET['search']) && ! empty($_GET['search']) {
//escape the input properly using your database-specific method, e.g.:
$searchParam = mysql_real_escape_string($_GET['search']);
//run your query with the escaped data
}
At a minimum, that would ensure that your passed in search variable was not an empty string.

Resources