Clean URL from custom $_GET variables in wordpress - php

I'm using wordpress pretty permalinks, my .htaccess file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /nafham/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /nafham/index.php [L]
</IfModule>
# END WordPress
I'm using a form that displays the index page with url variables, like this:
http://www.nafham.com/?edu_year_selectionreal=79&semester_selectionreal=15&subject_selectionreal=80
I'd like to have the url to be displayed like this:
http://www.nafham.com/79/15/80
Is it possible to do so? and what's the best way of doing this in order not to make things go wrong with the rewrite conditions wordpress is Already applying?

as you have the rewrite already setup all you need to do is parse the url in your index.php
if (preg_match("#(\d+)/(\d+)/(\d+)#", $_SERVER['REQUEST_URI'], $regs)) {
$edu_year_selection_real = $regs[1]
$semester_selectionreal = $regs[2];
$subject_selectionreal = $regs[3];
}

add smth. like this to .htaccess:
RewriteRule ^/([0-9-]+)/([0-9-]+)/([0-9-]+)$ index.php?var1=$1&var2=($2)&var3=($3) [L]

At you need to set custom rewrite rules in the Wordpress otherwise Wordpress throw you to error page.
Here's example to set custom rewrite rules:
<?php
add_filter('rewrite_rules_array', 'insert_custom_rules');
function insert_custom_rules($rules)
{
$newrules = array();
$newrules['(.+?)/(.+?)/(.+?)/?$'] =
'index.php?edu_year_selectionreal=$matches[1]&semester_selectionreal=$matches[2]&subject_selectionreal=$matches[3]';
return $newrules + $rules;
}
?>
Hope this will help.

Related

mod_rewrite query strings returning 404

I have a custom URL like this:
website.com/show/?id=9999&n=page-name
I'm trying to come up with a rewrite rule to convert it to:
website.com/show/9999/page-name/
Note that this is a WordPress site and /show/ is WP page name.
Here's the rules I'm using in .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^show/(.*)$ /show/?id=$1 [R=301,NC,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is working; it rewrites website.com/show/?id=9999 to website.com/show/9999/.
Then I modified the rule for the second query string:
RewriteRule ^show/(.*)/(.*)$ /show/?id=$1&n=$2 [R=301,NC,QSA]
But now website.com/show/9999/page-name/ returns a 404 error. It works if I go to: website.com/show/9999/?n=page-name.
What am I doing wrong?
Update
The 404 problem is now solved.
However, now I need to redirect the old query string URL:
website.com/show/?id=9999&n=page-name
to the new SEO friendly URL:
website.com/show/9999/page-name
How do I setup that redirect?
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^show/(\d+)/?([^/]*)/?$ /show/?id=$1&n=$2 [L,NC,QSA]
# Wordpress defaults:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
## Results
# show/9999 => show/?id=9999&n=
# show/9999/ => show/?id=9999&n=
# show/9999/page-name => show/?id=9999&n=page-name
# show/9999/page-name/ => show/?id=9999&n=page-name
As you see, when page-name is not available, there will be an empty n query string parameter. In your PHP script, instead of checking for the parameter presence using isset(), try using empty().
One last note; The above rules do not redirect the request, it maps the request under the hood, so that the URL stays clean. If you need a redirect, just add a R flag.
404
Even if you remove your custom rewrite rules, you'll get a 404 from WordPress. That's because, as you said, the rewrite target (/show) is a WordPress page and ultimately get mapped to the index.php file. Then, WordPress checks its database to see if it can find a page with that path or throws a 404 if it can't. Obviously, the latter is your case.
Update
Regarding your recent update; To redirect the old URL to the new one, you need some rewrite rules along the lines of:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=(\d+)(&n=)?(.*)$
RewriteRule ^show/?$ /show/%1/%3 [R=301,QSD,L]
# Wordpress default rules:
# ...
</IfModule>
## Results
# show?id=9999 => show/9999
# show?id=9999&n=page-name => show/9999/page-name
# show/?id=9999&n=page-name => show/9999/page-name
Don't use [R=301] until you're 100% sure that everything's working fine, as it's aggressively get cached by the browser.
I think I figured it out... almost.
#sepehr is correct that WordPress is checking, not finding the page, and throwing a 404. So I needed to use WordPress' own rewrite engine to define the rewrite rules so it recognizes what I'm doing.
So I added this to my WordPress theme's functions.php file:
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^show/([^/]*)/([^/]*)/?',
'index.php?page_id=2382&id=$matches[1]&n=$matches[2]',
'top'
);
}
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'id';
$vars[] = 'n';
return $vars;
}
Now the URL website.com/show/9999/page-name works correctly, not throwing a 404.
However, now I need to redirect the old query string URL to this new one. See my updated question.
Update
Here's the rewrite rule to redirect the old query string URLs to the new SEO friendly URLs:
RewriteCond %{QUERY_STRING} ^id=([^/]*)&n=([^/]*)$
RewriteRule ^show/?$ /show\/%1\/%2\/? [R=301,L]
Please check below rule in to your .htaccess.
RewriteRule ^show/(.+)/(.+)$ show/?id=$1&n=$2 [L,QSA]

CodeIgniter 3 Routing Issue

I used CI's routes and have the following entry in config/routes.php
if($subdomain == 'debug')
{
$route['debug/(:any)'] = "admin/debug/$1";
}
elseif($subdomain == 'dev')
{
$route['models/(:any)'] = "v2/dev/models/$1";
}
If I were to browse to
dev.mydomain.com/models/test/
everything works fine and I get the correct output.
However, I were to browse to
dev.mydomain.com/models/test/trip_id/9091
I will get a 404 Error page.
Only way to make it to work is to use the following route entry.
$route['models/(:any)/(:any)/(:any)'] = "v2/dev/models/$1/$2/$3";
What really confuses me further is that it works if I were to browse to
debug.mydomain.com/debug/test/trip_id/9091
What am I missing here as the route entries looks similar but will only work on the first subdomain (debug.mydomain.com) but not on the latter (dev.mydomain.com)
This is the content of my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help is greatly appreciated.
$route['models/(:any)'] = "v2/dev/models/$1";
Check the route path if you are calling the controller from the folder
it should be yourfolder/controller/method_name/$1

Adding route rule to Wordpress HTACCESS and keep the URL

I want to add a rule to my .htaccess file that doesn't follow the Wordpress default rules.
Everything like this:
1- /course/(:any)
Must be redirected to this:
2- /course/?slug=$1
But I want to keep the URL in 1, without the query string.
Everything I try either gives me an internal server error or doesn't match the rule. This is what I got and is just being ignored (because the wordpress shows me a 'page not found' message). The resulting URL is like http://localhost/bethmoda/courses/?slug without the parameter and changing the URL
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/course/(.*)$ /bethmoda/course?slug=$2 [L]
RewriteBase /bethmoda/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /bethmoda/index.php [L]
</IfModule>
# END WordPress
How can I do this?
You can use the following rule :
RewriteEngine On
#Exclude /bethmoda/course/index.php
RewriteCond %{REQUEST_URI} !^/bethmoda/course/index\.php$
RewriteRule ^/?bethmoda/course/(.+)$ /bethmoda/course/?slug=$1 [NC,L]
The rewriteCond is important to avoid RewriteLoop error on the second rewrite iteration, otherwise without this condition You will get an Internal server error.
The following is a wordpress way to achieve what you want.
If possbile: change "slug" with something else, lets say "course-slug".
//Add query variable, so that wordpress identifies your custom query string
function add_custom_query_var( $qvars ) {
$qvars[] = 'course-slug';
return $qvars;
}
add_filter('query_vars', 'add_custom_query_var');
Add your custom rewrite rules
//Assumption: course is a page
add_filter( 'rewrite_rules_array', 'custom_rewrite_rules' );
function custom_rewrite_rules( $rewrite_rules )
{
$pattern = "course/([^/]+)?$";
$target = "index.php?" . "name=course&course-slug=\$matches[1]";
// If course was a custom post type
// $target = "index.php?" . "post_type=course&course-slug=\$matches[1]";
// If course was a category
// $target = "index.php?" . "category_name=course&course-slug=\$matches[1]";
// If course was a tag
// $target = "index.php?" . "tag=course&course-slug=\$matches[1]";
$newRules[$pattern] = $target;
return array_merge($newRules,$rewrite_rules);
}
Flush permalink
Step 1: In the main menu find "Settings > Permalinks".
Step 2: Scroll down if needed and click "Save Changes".
Step 3: Rewrite rules and permalinks are flushed.

URL $_GET will not work

I have a problem I never had and I can't find the reason.
I moved my site to another host and now it doesnt "read" the $_GET variables.
I have this url: http://whatever.com/path?filtro=si&provincia=Santa+Fe&localidad=Rosario
And if I call this:
$localidad = $_GET['localidad'];
$provincia = $_GET['provincia'];
$filtro = $_GET['filtro'];
echo $localidad;
echo "hola";
echo $provincia;
echo $filtro;
Nothing prints except "hola", so there is no PHP error.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Im working on a Wordpres site, perhaps it has something to do with Permalinks or something, Im really lost. Thank you very much, I appreciate your help.
EDIT
I renamed my .htacces so it wont read it and the page broke, so I went to permalink settings in wordpress and set them to
- Post Name http://luminias.com/index.php/example-page/
And now IT WORKS, but, now this is thw url:
http://whatever.com/index.php/path/?filtro=si&provincia=Santa+Fe&localidad=Rosario
And it prints all the $_GET, but I need that "/index.php/" gone..
Add add_rewrite_tag function in your function.php for all parameter :
function custom_rewrite_tag() {
add_rewrite_tag('%localidad%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
And you can call your parameter in template using
$wp_query->query_vars['localidad']
Here is the full documentation
Note that using $_GET on a rewritten URL will not work, even if the
rewrite includes the querystring variables. You must use $wp_query.

Rewrite URLs for SEO friendly with .htaccess on Wordpress

I'm developing a website using Wordpress (for articles, static pages, ...) but in my theme folder I created a subsystem (handled via php-mysql that doesn't require Wordpress) that uses another database (that contains products). The php page of a single product (product.php) is this:
<?php
...
if($_GET['url']){
$url = $_GET['url'];
$sql = "select * from `products` where url='$url'";
$row = // start the query ($sql);
$title=$row[0]['title'];
$body=$row[0]['body'];
}
else{echo '404 Page.';}
?>
<body>
<?php
echo "<h1>$title</h1>
<p>$body</p>";
?>
</body>
And it's rewritten by Wordpress to product/ in the URL, infact, opening (i.e.) this URL it works:
http://localhost:1234/my-site/product/?url=product-number-one
^
product.php
but I need to rewrite it to this
http://localhost:1234/my-site/product/product-number-one
.htaccess on Wordpress is by default this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my-site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my-site/index.php [L]
</IfModule>
# END WordPress
I added to that this rule:
RewriteRule ^(.*) product?url=$1 [R]
but doesn't work. I tried others solutions but nothing done.
What you can do is very simple:
RewriteRule ^my-site/product/([A-Za-z0-9-]+)$ /my-site/filename_of_product_page.php?url=$1 [L]
With the expression listed within the parentheses, you would need to be sure your url itself matches this- so use a scrubber function when inserting the name into the database.

Categories