Title: Rewrite Problem
Last modified: February 4, 2021

---

# Rewrite Problem

 *  Resolved [peperoni0815](https://wordpress.org/support/users/peperoni0815/)
 * (@peperoni0815)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/rewrite-problem-3/)
 * Hi,
 * i have a little Problem. I want a URL like this:
    [https://www.example.org/jobdetails/web-develeoper/213123-3rrf43r-3r23rr2](https://www.example.org/jobdetails/web-develeoper/213123-3rrf43r-3r23rr2)(
   SEO Friendly)
 * I have 2 Custom Site-Templates:
    1. Job Overview (A Datatable Table with Data
   from an JSON File) works fine! 2. Job Details (Get Data from a REST-API Parameter:
   jobid) works fine too!
 * My Problem:
    The Link to the Jobdetails is like this: [https://example.org/jobdetails/?1e1515f7-4ee9-4c63-a199-2964a5f3d849](https://example.org/jobdetails/?1e1515f7-4ee9-4c63-a199-2964a5f3d849)
 * I Want this URL:
    [https://example.org/jobdetails/JOBTITLE/1e1515f7-4ee9-4c63-a199-2964a5f3d849](https://example.org/jobdetails/JOBTITLE/1e1515f7-4ee9-4c63-a199-2964a5f3d849)
   JOBTITLE = Human/SEO Friendly Jobtitle like: “Web-developer-Munich” Example: 
   [https://example.org/jobdetails/Web-developer-Munich/1e1515f7-4ee9-4c63-a199-2964a5f3d849](https://example.org/jobdetails/Web-developer-Munich/1e1515f7-4ee9-4c63-a199-2964a5f3d849)
 * When i try my best, i always got the 404 Error 🙁
 * functions.php in my Child Theme Folder
 *     ```
       add_action( 'init',  function() {
           add_rewrite_rule( '[^\/]+$', 'jobdetails/?jobid=$matches[1]', 'top' );
       } );
   
       add_filter( 'query_vars', function( $query_vars ) {
           $query_vars[] = 'jobid';
           return $query_vars;
       } );
       ```
   
 * I hope anybody understands me an can kick me in the right lane… 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/rewrite-problem-3/#post-14004479)
 * Your regexp is too generic and doesn’t capture anything that will be placed in
   $matches. It seems you have little regexp experience, which is OK, it’s not that
   easy to learn about. Its syntax is exacting and often non-intuitive. I recommend
   you use a site like regexr.com to help you come up with workable regexp. You 
   can place an example URL in the sample text, then fiddle with the regexp until
   it captures the data you need in $matches. It has a minimal reference feature
   to remind you about what regexp symbols do what.
 * Your regexp needs to match a static element like “jobdetails”, and capture the
   subsequent two elements into $matches. We use `()` to capture portions of the
   matched string.
 * Review this docs page for more information:
    [https://developer.wordpress.org/reference/functions/add_rewrite_rule/](https://developer.wordpress.org/reference/functions/add_rewrite_rule/)
   The user notes and examples should be helpful to you.
 * Don’t forget to visit the permalinks settings screen anytime you alter rewrite
   rules.
 *  Thread Starter [peperoni0815](https://wordpress.org/support/users/peperoni0815/)
 * (@peperoni0815)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/rewrite-problem-3/#post-14009045)
 * Hi bcworkz,
 * thank you for your answer, yes my regexp is very low, but the regexp in my case
   are worke fine: [https://regexr.com/5lrlv](https://regexr.com/5lrlv)
 * My problem is: Wordpres thinks i want surf to a other Site… i got the error 404…
 * How can i say to my wordpress installation i want a Site domain.com/example and
   ignores the rest of the URL… also domain.com/example/BLAH/BLUB <— wordpress looks
   4 a Site example -> BLAH -> BLUB….
 * i hope you understand me….
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/rewrite-problem-3/#post-14012592)
 * Your regexp works too well. It traps normal post and page requests which are 
   meant for other rules. It needs to be more discriminating, only matching requests
   that contain “jobdetails”. It also does not capture anything for use in $matches
   and similar place holders.
 * Also, if you don’t rewrite to index.php, from the docs page:
 * > The $redirect argument works slightly differently when redirecting to a custom
   > PHP script because WordPress delegates these redirects to .htaccess instead
   > of processing them itself. For this reason, querystring variables should be
   > written like $1 instead of $matches[1]. Given we’re redirecting to a custom
   > PHP script, adding the same rewrite rule from our previous example would look
   > like this:
 *     ```
       function custom_rewrite_rule() {
           add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','url/to/my/script.php?food=$1&variety=$2','top');
       }
       add_action('init', 'custom_rewrite_rule', 10, 0);
       ```
   
 * Also note that the string being tested is not a full URL, the domain portion 
   is stripped. You’re really matching something like “jobdetails/DKS4L676/”
 *  Thread Starter [peperoni0815](https://wordpress.org/support/users/peperoni0815/)
 * (@peperoni0815)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/rewrite-problem-3/#post-14029789)
 * now i have solved my problem.
 * thank you for your patience!
 * The problem was that the forwarding must of course be directed to the index.php
   with the name of the static page.
 *     ```
       function custom_rewrite_rule() {
           #add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','url/to/my/script.php?food=$1&variety=$2','top');
       	add_rewrite_rule( '^stellenanzeige\/([^/]*)\/([^/]*)\/?', 'index.php?pagename=stellenanzeige&jobid=$matches[2]', 'top' );
       }
       add_action('init', 'custom_rewrite_rule', 10, 0);
   
       add_filter( 'query_vars', function( $query_vars ) {
           $query_vars[] = 'jobid';
           return $query_vars;
       } );
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Rewrite Problem’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [peperoni0815](https://wordpress.org/support/users/peperoni0815/)
 * Last activity: [5 years, 4 months ago](https://wordpress.org/support/topic/rewrite-problem-3/#post-14029789)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
