@@ -1330,22 +1330,6 @@ function preg_index($number, $matches = '') {
13301330}
13311331
13321332
1333- function page_permastruct () {
1334- $ permalink_structure = get_settings ('permalink_structure ' );
1335-
1336- if (empty ($ permalink_structure )) {
1337- return '' ;
1338- }
1339-
1340- $ index = 'index.php ' ;
1341- $ prefix = '' ;
1342- if (using_index_permalinks ()) {
1343- $ prefix = $ index . '/ ' ;
1344- }
1345-
1346- return '/ ' . $ prefix . 'site/%pagename% ' ;
1347- }
1348-
13491333function get_page_uri ($ page ) {
13501334 global $ wpdb ;
13511335 $ page = $ wpdb ->get_row ("SELECT post_name, post_parent FROM $ wpdb ->posts WHERE ID = ' $ page' " );
@@ -1374,6 +1358,68 @@ function page_rewrite_rules() {
13741358 return $ rewrite_rules ;
13751359}
13761360
1361+ function get_date_permastruct ($ permalink_structure = '' ) {
1362+ if (empty ($ permalink_structure )) {
1363+ $ permalink_structure = get_settings ('permalink_structure ' );
1364+
1365+ if (empty ($ permalink_structure )) {
1366+ return false ;
1367+ }
1368+ }
1369+
1370+ $ front = substr ($ permalink_structure , 0 , strpos ($ permalink_structure , '% ' ));
1371+ // The date permalink must have year, month, and day separated by slashes.
1372+ $ endians = array ('%year%/%monthnum%/%day% ' , '%day%/%monthnum%/%year% ' , '%monthnum%/%day%/%year% ' );
1373+
1374+ $ date_structure = '' ;
1375+
1376+ foreach ($ endians as $ endian ) {
1377+ if (false !== strpos ($ permalink_structure , $ endian )) {
1378+ $ date_structure = $ front . $ endian ;
1379+ break ;
1380+ }
1381+ }
1382+
1383+ if (empty ($ date_structure )) {
1384+ $ date_structure = $ front . '%year%/%monthnum%/%day% ' ;
1385+ }
1386+
1387+ return $ date_structure ;
1388+ }
1389+
1390+ function get_year_permastruct ($ permalink_structure = '' ) {
1391+ $ structure = get_date_permastruct ($ permalink_structure );
1392+
1393+ if (empty ($ structure )) {
1394+ return false ;
1395+ }
1396+
1397+ $ structure = str_replace ('%monthnum% ' , '' , $ structure );
1398+ $ structure = str_replace ('%day% ' , '' , $ structure );
1399+
1400+ $ structure = preg_replace ('#/+# ' , '/ ' , $ structure );
1401+
1402+ return $ structure ;
1403+ }
1404+
1405+ function get_month_permastruct ($ permalink_structure = '' ) {
1406+ $ structure = get_date_permastruct ($ permalink_structure );
1407+
1408+ if (empty ($ structure )) {
1409+ return false ;
1410+ }
1411+
1412+ $ structure = str_replace ('%day% ' , '' , $ structure );
1413+
1414+ $ structure = preg_replace ('#/+# ' , '/ ' , $ structure );
1415+
1416+ return $ structure ;
1417+ }
1418+
1419+ function get_day_permastruct ($ permalink_structure = '' ) {
1420+ return get_date_permastruct ($ permalink_structure );
1421+ }
1422+
13771423function generate_rewrite_rules ($ permalink_structure = '' , $ matches = '' ) {
13781424 $ rewritecode =
13791425 array (
@@ -1519,15 +1565,8 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
15191565 $ prefix = $ index . '/ ' ;
15201566 }
15211567
1522- // If the permalink does not have year, month, and day, we need to create a
1523- // separate archive rule.
1524- $ doarchive = false ;
1525- if (! (strstr ($ permalink_structure , '%year% ' ) && strstr ($ permalink_structure , '%monthnum% ' ) && strstr ($ permalink_structure , '%day% ' )) ||
1526- preg_match ('/%category%.*(%year%|%monthnum%|%day%)/ ' , $ permalink_structure )) {
1527- $ doarchive = true ;
1528- $ archive_structure = $ front . '%year%/%monthnum%/%day%/ ' ;
1529- $ archive_rewrite = generate_rewrite_rules ($ archive_structure , $ matches );
1530- }
1568+ // Generate date rules.
1569+ $ date_rewrite = generate_rewrite_rules (get_date_permastruct ($ permalink_structure ), $ matches );
15311570
15321571 // Site feed
15331572 $ sitefeedmatch = $ prefix . 'feed/?([_0-9a-z-]+)?/?$ ' ;
@@ -1572,12 +1611,7 @@ function rewrite_rules($matches = '', $permalink_structure = '') {
15721611 $ pages_rewrite = page_rewrite_rules ();
15731612
15741613 // Put them together.
1575- $ rewrite = $ pages_rewrite + $ site_rewrite + $ page_rewrite + $ search_rewrite + $ category_rewrite + $ author_rewrite ;
1576-
1577- // Add on archive rewrite rules if needed.
1578- if ($ doarchive ) {
1579- $ rewrite = $ rewrite + $ archive_rewrite ;
1580- }
1614+ $ rewrite = $ pages_rewrite + $ site_rewrite + $ page_rewrite + $ search_rewrite + $ category_rewrite + $ author_rewrite + $ date_rewrite ;
15811615
15821616 $ rewrite = $ rewrite + $ post_rewrite ;
15831617
@@ -1598,15 +1632,20 @@ function mod_rewrite_rules ($permalink_structure) {
15981632 $ rules .= "RewriteEngine On \n" ;
15991633 $ rules .= "RewriteBase $ home_root \n" ;
16001634 $ rewrite = rewrite_rules ('' , $ permalink_structure );
1635+
1636+ $ num_rules = count ($ rewrite );
1637+ $ rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR] \n" .
1638+ "RewriteCond %{REQUEST_FILENAME} -d \n" .
1639+ "RewriteRule ^.*$ - [S= $ num_rules] \n" ;
1640+
16011641 foreach ($ rewrite as $ match => $ query ) {
16021642 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
16031643 $ match = str_replace ('.+? ' , '.+ ' , $ match );
16041644
16051645 // If the match is unanchored and greedy, prepend rewrite conditions
16061646 // to avoid infinite redirects and eclipsing of real files.
1607- if ($ match == '(.+)/?$ ' ) {
1608- $ rules .= "RewriteCond %{REQUEST_FILENAME} !-f \n" .
1609- "RewriteCond %{REQUEST_FILENAME} !-d \n" ;
1647+ if ($ match == '(.+)/?$ ' || $ match == '([^/]+)/?$ ' ) {
1648+ //nada.
16101649 }
16111650
16121651 if (strstr ($ query , 'index.php ' )) {
0 commit comments