• Hello WordPress community!

    I’m tackling a task of writing a theme in the MVC style. The theme I’m working on is inspired by Laravel so a lot of the file and folder structures have been borrowed.

    So the issue I’m having is that I need WordPress to edit the .htaccess file from the functions.php file, if that’s possible?

    Consider the following url,

    https://www.website.com/controller/method/params/

    I need WordPress to redirect this url to index.php?url=controller/method/params/
    Essentially, everything after “www.website.com/” is the url parameter.

    I have managed to solve this by manually editing the .htaccess file so that it looks like this

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.+)$ index.php?url=$1 [L]
    
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    This seems to work. I can still access files like wp-login.php without the page breaking, and I have access to the url parameter. However, I don’t want to manually change this file.

    Is there a way for WordPress to change this for me?

    I should add that I’ve tried to do this with the WP_Rewrite object, but failed. Although I managed to add my custom rewrite rule to the object, it didn’t apply to the htaccess.

    Help is appreciated.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, it’s possible. PHP has a number of file input/output read/write open/close sort of functions that you can use. They will work as well with .htaccess as with any other file, provided the permissions are properly set. What’s proper depends on the installation. You need to have a fall back plan in case .htaccess is not accessible. What WP does in this case is not allow pretty permalinks.

    Do not edit inside the WP entry. Your edit will be lost when permalink or rewrite settings are changed. Create your own block for your own use.

    I’d encourage you to pursue a Rewrite API solution though. It can be a little tricky to get working right. Once you get it, you’ll be much better off than trying to modify .htaccess. For most rewrite rules, WP does not need to alter .htaccess. The vast majority of rewrites are handled internally, no .htaccess changes are needed. If a rewrite rule is added that does require changing .htaccess, WP will handle it.

Viewing 1 replies (of 1 total)
  • The topic ‘Writing a theme in the style of MVC’ is closed to new replies.