Dominik
Dominik

Reputation: 31

wordpress and custom htaccess rules

I am looking over my code for 3 hours and I don't get it. I hope that you can help me.

I have a wordpress blog with Custom Post Types and Advanced Custom Fields installed. Its working pretty nice. I use a basic filter function over the custom fields as described here: http://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

To filter the custom fields I use the get-parameters form the url. So the url comes in the form like:

www.domain.tld/?post_type=movie&land=usa&typ=love&actor=kidman

Now I would like the have a pretty url in the form:

www.domain.tld/movie/usa/love/kidman

And hier comes the issue, I cant get the .htaccess right :(

My Code is:

AddHandler php56-cgi .php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.*)/(.*)/(.*)$ /?post_type=movie&land=$1&typ=$2&actor=$3 [L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

It's not working. The result is a 404. I believe is's a "wordpress-thing" that I don't get.

If I change the code to:

RewriteRule ^movie/(.*)/(.*)/(.*)$ http://www.domain.tld/?post_type=movie&land=$1&typ=$2&actor=$3 [L]

It works as a normal redirect.

If I change it to

RewriteRule ^movie/(.*)/(.*)/(.*)$ file.php/?post_type=movie&land=$1&typ=$2&actor=$3 [L]

where file.php is a non wordpress file, it works as well (It shows the content of file.php)

It seems that I'am not able to "call" a wordpress-specific file like index.php.

I hope you can help me, and thanks that you read through my non native english ;)

Greetings from germany Dominik

Upvotes: 1

Views: 1813

Answers (1)

Dominik
Dominik

Reputation: 31

I solved my problem on my own :)

As always, the solution is pretty simple if know where to look.

I had a deep look into: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule and http://codex.wordpress.org/Rewrite_API/add_rewrite_tag Finally the coin dropped.

First I deleted every custom code I made in the .htaccess cause its not the location to solve my problem.

I added the following to my functions.php

function custom_rewrite_basic() {
  add_rewrite_rule('^movie/(.*)/(.*)/(.*)?', 'index.php?post_type=movie&land=$matches[1]&typ=$matches[2]&actor=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');

function custom_rewrite_tag() {
  add_rewrite_tag('%land%', '([^&]+)');
  add_rewrite_tag('%typ%', '([^&]+)');
  add_rewrite_tag('%actor%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

After you edited your function.php don't forget to save the permalinks in your wordpress settings.

Now I have access to the "query_vars" in the template:

$wp_query->query_vars['land']

or the functions.php:

$query->query_vars[ land ]

It works perfect :)

Thank u anyway and have a nice weekend Dominik

Upvotes: 2

Related Questions