0

I am having a strange issue with permalinks while using the /%postname%/ structure.

I can access my custom post types using the pretty permalink localhost/cpu/<post-type>/<post-title>/ but I am getting a 404 error on Pages. The url of a page is localhost/cpu/<page-title>/

My .htaccess file (generated automatically when saving permalinks):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cpu/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cpu/index.php [L]
</IfModule>

# END WordPress

My WordPress(version 3.4.2) installation is in a folder called cpu. I don't have any issues with the default permalink structure url/?p=123. The problem does not go away when I disable all plugins.

Any possible solution?

Is there a way to debug what's happening?

UPDATE:

Following Code Monkey's answer, I added the following to functions.php I changed the filter from rewrite_rules_array to page_rewrite_rules - the WP_Rewrite Class reference suggests:

To filter the rewrite rules generated for your Pages, use page_rewrite_rules.

So, here's the code I added:

add_filter('page_rewrite_rules', 'rewrite_rules_array_filter_debug', 10000);

function rewrite_rules_array_filter_debug($rules){
  echo '<pre>';  
  var_dump($rules);
  echo '</pre>';
  return $rules;
}

And the output from saving the post permalinks on wp-admin/options-permalink.php:

array(11) {
  [".?.+?/attachment/([^/]+)/?$"]=>
  string(32) "index.php?attachment=$matches[1]"
  [".?.+?/attachment/([^/]+)/trackback/?$"]=>
  string(37) "index.php?attachment=$matches[1]&tb=1"
  [".?.+?/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
  [".?.+?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
  [".?.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?$"]=>
  string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]"
  ["(.?.+?)/trackback/?$"]=>
  string(35) "index.php?pagename=$matches[1]&tb=1"
  ["(.?.+?)/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(47) "index.php?pagename=$matches[1]&feed=$matches[2]"
  ["(.?.+?)/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(47) "index.php?pagename=$matches[1]&feed=$matches[2]"
  ["(.?.+?)/page/?([0-9]{1,})/?$"]=>
  string(48) "index.php?pagename=$matches[1]&paged=$matches[2]"
  ["(.?.+?)/comment-page-([0-9]{1,})/?$"]=>
  string(48) "index.php?pagename=$matches[1]&cpage=$matches[2]"
  ["(.?.+?)(/[0-9]+)?/?$"]=>
  string(47) "index.php?pagename=$matches[1]&page=$matches[2]"
}

Anything wrong in it?

UPDATE 2:

I found my error when using var_dump($wp);.

I got this response:

  ["query_string"]=>
  string(25) "statut-membre=sample-page"
  ["request"]=>
  string(11) "sample-page"
  ["matched_rule"]=>
  string(10) "([^/]+)/?$"
  ["matched_query"]=>
  string(25) "statut-membre=sample-page"
  ["did_permalink"]=>
  bool(true)

Wordpress was querying a taxonomy called statut-membre while looking for my pages. I noticed I had an empty rewrite slug('rewrite' => array('slug' => ''),) in that register_taxonomy function. I added a slug and resaved my permalink structure, now my I can access my pages.

Now the query looks fine too:

  ["query_string"]=>
  string(20) "pagename=sample-page"
  ["request"]=>
  string(11) "sample-page"
  ["matched_rule"]=>
  string(20) "(.?.+?)(/[0-9]+)?/?$"
  ["matched_query"]=>
  string(26) "pagename=sample-page&page="
  ["did_permalink"]=>
  bool(true)

2 Answers 2

1

can you try replace

RewriteRule . /cpu/index.php [L]

by

RewriteRule . /index.php [L]

as for a way to debug...

well you can always var_dump a $wp_query object to get an idea what variables not passed to wp. (you can place ) into first line of your header.php template.

and i belive next step will be debug of rewrites in this case usefull filter is rewrites_array_rules which you can use for debug proposes in a next way

add_filter('rewrite_rules_array', 'rewrite_rules_array_filter_debug', 10000);
    function rewrite_rules_array_filter_debug($rules){
    var_dump($rules);
    return $rules;
}

add this to header php

   <?php 
          global $wp;
          echo '<pre>';
          var_dump($wp);
          echo '</pre>';
   ?>

this shoudl give you a point what rewrite rulle trigger to you page request.

3
  • I get redirected to localhost/xampp after rewriting the .htaccess as you suggested. I reverted back to my initial configuration. I'm trying your function, but I'm not getting any output right now.
    – RRikesh
    Dec 4, 2012 at 9:31
  • Oh it the filter upon saving wp-admin/options-permalink.php? I'm updating my question with the results.
    – RRikesh
    Dec 4, 2012 at 12:03
  • one more debug code added. Dec 4, 2012 at 12:30
1

You would want to tell apache to follow your .htaccess file. You can do this by editing the apache.conf file

$sudo nano /etc/apache2/apache.conf

Scroll down to the line By default it will be:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

change the value of AllowOverride to All so now it becomes:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Press ctrl+x and press y to save the configuration file. In order to make this changes to server first enable the mod_rewrite by.

$ sudo a2enmod rewrite

And then restart the server

$ sudo service apache2 restart

Done!

Source: https://www.wst.space/riddling-with-wordpress-permalink-setup-issues/

1
  • I missed this while setting up my server, finally found the culprit. Good catch Jul 25, 2022 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.