-1

I'd like to use mod_rewrite to show pretty urls in my urls:

Instead of .../juegos/plants-vs-zombies/?play=jugar change to .../juegos/plants-vs-zombies/jugar/

And .../juegos/ddtank/?play=full change to .../juegos/ddtank/full/

I use the file "single.php" with this code:

$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$parts = parse_url($url);
parse_str($parts['query'], $query); 
$parametro = $query['play'];

if ($parametro == 'jugar')
{
    include( get_template_directory() . '/single-play.php');
}
else if ($parametro == 'full')
{
    include( get_template_directory() . '/single-full.php');
}

And in .htaccess I have this:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)play=jugar($|&)
RewriteRule ^(.*)/$ /jugar/?&%{QUERY_STRING}

But when I try to get the url with /jugar/ and /full/ at the end of the url, it displays an 404 error.

I don't know what else to do. I hope you can help me.

  • 4
    Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – user3942918 Aug 20 '17 at 17:06
  • 1
    Mod_rewrite doesn't work on PHP includes. – Brian Gottier Aug 20 '17 at 17:08
  • If Mod_rewrite doesn't work on PHP includes, do you know any other way to load the custom post file depending if the file single.php receive the parameter "play"? – Fernando Lozano Hernández Aug 20 '17 at 23:43
  • if it displays an 404 error. that means it thinks that full and jugar are directories @Fernando Lozano Hernández – Moaz Mabrok Aug 22 '17 at 13:53
  • did you try this http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ @Fernando Lozano Hernández – Moaz Mabrok Aug 22 '17 at 13:57

2 Answers2

0

Sounds pretty straight forward to me:

RewriteEngine on
RewriteRule ^/?juegos/plants-vs-zombies/jugar/?$ /juegos/plants-vs-zombies/?play=1 [END]

For that to work the rewriting module has to be installed and enabled, obviously. The rule will work likewise in the http servers host configuration and in a dynamic configuration file (.htaccess). If you decide to use a dynamic configuration file, then you need to place that in the http hosts document root folder and enable its interpretation using the AllowOverride directive in the host configuration.

If you receive a http status 500 using the above rule ("server internal error") chances are that you operate a very old version of the apache http server. In that case try replacing the [END] flag with the [L] flag.

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

arkascha
  • 37,875
  • 7
  • 50
  • 85
0

what the browser sees /juegos/plants-vs-zombies/jugar/,

what the server sees ?play=juegos/plants-vs-zombies/jugar

after explode it changes to an Array ( [0] => juegos [1] => plants-vs-zombies/ [1] => jugar)

 Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?play=$1 [NC]

This to change$_GET['play'] to an array

$play = explode("/", $_GET['play']);


 if(isset($_GET['play'])){
    $play = explode("/", $_GET['play']);

    //if you want to add `/` at the end
     if(end($play) == ""){
        array_pop($page);
    }
 }

Passing Query String Parameters in WordPress URL

Moaz Mabrok
  • 686
  • 6
  • 22