• I have a file called getpage.php that I want to access with a url like https://www.mysite.com/getpage/ instead of https://www.mysite.com/wp-content/custom-pages/getpage.php.

    when I use the following code as a plugin and navigate to https://www.mysite.com/getpage/ I get redirected to the homepage instead of the page I’m trying to request.

    function kk_rewrite_rules( $wp_rewrite ){
    	$new_rules = array('^getpage/?$' => 'wp-content/custom-pages/getpage.php');
    
    	// Add the new rewrite rule into the top of the global rules array
    	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }

    Any ideas? I think what might be happening is that wordpress makes everything go through index.php so if it’s not a $_GET request like index.php?mypage=getpage then I can’t use wp_rewrite to redirect to the proper page.

Viewing 1 replies (of 1 total)
  • Thread Starter desie314

    (@desie314)

    I seem to have found a solution. I simply modified my .htaccess file as follows:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^getpage/?$ wp-content/custom-pages/getpage.php [QSA,L]
    RewriteRule ^index\.php$ - [L]
    
    # uploaded files
    RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule . index.php [L]
    </IfModule>
    
    # END WordPress

    The only thing I did was add the following line:
    RewriteRule ^getpage/?$ wp-content/custom-pages/getpage.php [QSA,L]

    I have tried this before but it didn’t work. It may be the [QSA,L] that I found in a google search that makes it work this time.

    I was initially trying to add this line to htaccess internally with the use of non_wp_rules but this did not update the htaccess file. Not even after flushing the rules with $wp_rewrite->flush_rules(); or by saving the settings on the admin permalink settings page.

    I’m afraid that the htaccess file will be overwritten at some point and my rule will not be there.

Viewing 1 replies (of 1 total)
  • The topic ‘add rewrite rules for a non-wordpress php file’ is closed to new replies.