Question

I would love to add a Rewrite Rule to the existing Wordpress Rewrite block. I have a special Page which needs dynamic parameters but SEO friendly urls.

So

http://unitedathletes.ch/athleten/var_sportart/var_name

should become

http://unitedathletes.ch/athleten/?sportart=var_sportart&name=var_name

Whereas

http://unitedathletes.ch/athleten/

is an existing page, which contains the necessary logic to handle the query parameters.

The existing wordpress htaccess rewrite block looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On

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

</IfModule>

Where in that block and how could I add the wished rule? I tried quite some stuff but coulnd't figure it out on my own.

update 1

Trying add_rewrite_rule(), can't make it work: in functions.php:

add_action( 'init', 'add_rewrite' );
function add_rewrite(){
    add_rewrite_rule('^athleten/([^/]*)/([^/]*)/?','index.php?page_id=340&sportart=$matches[1]&athlet=$matches[2]','top');
    add_rewrite_tag('%sportart%','([^&]+)');
    add_rewrite_tag('%athlet%','([^&]+)');
}

update 2

Just needed to flush the rewrite rules. Thanks for all the help.

flush_rewrite_rules();
Was it helpful?

Solution 3

Add this to functions.php

add_action( 'init', 'add_rewrite' );
function add_rewrite(){
    add_rewrite_rule('^athleten/([^/]*)/([^/]*)/?','index.php?page_id=340&sportart=$matches[1]&athlet=$matches[2]','top');
    add_rewrite_tag('%sportart%','([^&]+)');
    add_rewrite_tag('%athlet%','([^&]+)');
    flush_rewrite_rules();
}

No need to dabble with .htaccess

OTHER TIPS

The tokens in your description do not match the tokens in your example code at all. It is a mixture of terms and languages. Maybe that is all that has to be fixed, so here is a try. But unless you specify more details and what your actual problem is there is little we can do to help...

RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^members/([^/]+)/([^/]+)$ /members/?sporttype=$1&name=$2 [L,NC,QSA]

Once more, this is just a rough guess, since the are important details missing in your question.

And a general note which I always attach when people use .htaccess style files: if you have access to the server configuration, then always place such rules inside the host configuration. .htaccess style files are notoriously error prone, they make things complex, are hard to debug and really slow the server down. The only reason to use such files is if you have to modify the rules on a dynamic base or if you have no access to / no control over the server configuration.

Changing

RewriteRule ^athleten/([^/]*)/([^/]*)$ /?page_id=295&sportart=$1&athlet=$2 [L,NC]

to

RewriteRule ^members/([^/]*)/([^/]*)$ /members/?&sporttype=$1&name=$2 [L,NC]

should work.

You wanted the end result to be domain.com/members/?sporttype=sporttype&name=name_familyname but you were turning athleten/sporttype/name_familyname into /?page_id=295&sportart=$1&athlet=$2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top