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

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.

Related

Liferay friendly url with optional param

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

Use app.get for variable and null

I'm writing a middle-layer for an existing webpage. I want a single controller that does the same thing for all urls and also allows me to capture the first parameter. I have the following urls:
/
/ask
/1234
/1234/slug-for-page
1234 is a random number
I know I can do
app.get('/:method', parse);
to capture "ask" and "/1234", but I need something that allows me to capture all of the samples above and also captures the variables.

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');

.htaccess geolocation without mod_geoip

I'm trying to figure out a method to do the following:
Include a "lang token" into ALL .htaccess rewrited-urls, like this:
RewriteRule ^/?$ _main_htaccess_handler?mod=homepage&lang=[LANG_STRING]
RewriteRule ^disclaimer\.htm?$ _main_htaccess_handler?mod=disclaimer&lang=[LANG_STRING]
... and so on, for all the urls
All countries EXCEPT Italy:
[LANG_STRING] = 'EN';
Country ITALY
[LANG_STRING] = 'IT';
Now, searching for a method to accomplish this, I've found this script:
http://ipinfodb.com/country_query.php?country=IT&output=htaccess_allow
This will output a list of IPs allocated to a country, with a 99.5% accuracy.
I would like to "link" those IP subnet strings to the .htaccess type of approach mentioned above.
Anyway, I'm a little bit confused about geolocalization via .htaccess without the mod_geoip.
Do you think the solution I've "created" and mentioned above can work good? And then, what about search engines? Will, for example, Google Italy correctly find pages in Italian? (this question applies to all search engines)
Thank you very much
Having a huge .htaccess file is never a good idea, since the file is parsed before every request to the given website, resulting in loss of performance.
After having tried some techniques, I've opted for an hybrid one: PHP + htaccess
I'm writing it for the ones that will fly into this question.
HTACCESS CONFIGURATION:
RewriteRule ^/?$ _builder.php?mod=language-redirect [L,QSA]
RewriteRule ^en/?$ _builder.php?mod=homepage&lang=en [L,QSA]
RewriteRule ^it/?$ _builder.php?mod=homepage&lang=it [L,QSA]
_builder.php CODE ( relative to Italian and English Language, easily convertible to suit your needs )
if ( ! isset ( $_GET['lang'] ) || ( $_GET['lang'] != 'en' && $_GET['lang'] != 'it' ) ) {
// reaches here when someone accesses the / of the site. This piece of code outputs the correct geolocation language. If needed, you can also place an header redirect to the correct language-based homepage instead of defining the language
$geoplugin = new geoPlugin();
if ( $geoplugin -> countryCode == 'IT' ) define ( 'LANGUAGE', 'it' );
else define ( 'LANGUAGE', 'en' );
}
else {
if ( $_GET['lang'] == 'it' ) define ( 'LANGUAGE', 'it' );
else define ( 'LANGUAGE', 'en' );
}
Finally, the geoPlugin Class is reachable through Google "PHP geoplugin" or by this url:
http://www.geoplugin.com/webservices/php
Remember to place the language setter snippet into your main builder, so the script can know what language to output on the page, even if that wasn't passed by htaccess.

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