wordpress htaccess how to remove all the php extension except index.php

I have a WordPress blog and I want to add some custom page in it and remove the .php extension and querystring from urls.

All WordPress page urls are like index.php?pagename=, so if I tried add RewriteRule ^(.*)$ $1.php in the .htaccess file, the other pages return 500 internal server error. How can I remove the php extension from everything except index.php and login.php?

Read More

Here is my existing .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# RewriteRule ^(.*)$ $1.php  # add this line will broken the wordpress rewrite rule
</IfModule>
# END WordPress

Related posts

Leave a Reply

4 comments

  1. If you want add, for example, few custom php pages, you should add your own rewrites before standard WordPress part, but after RewriteBase /.

    Here I have added a couple of rewrites able to handle custom pages stored inside in a private directory custom.

        # BEGIN WordPress
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # *** START CUSTOM PART ***
        RewriteRule ^page1$     /custom/page1.php    [L,QSA]
        RewriteRule ^page2$     /custom/page2.php    [L,QSA]
        # *** END CUSTOM PART ***
    
        RewriteRule ^index.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        </IfModule>
        # END WordPress
    

    If your site is available on http://www.example.com, your custom pages will be accessible using http://www.example.com/page1, http://www.example.com/page2 and so on…

    Just few words regarding rewrite flags [L,QSA]:

    • L – If rewrite match, stop the rewriting process immediately and don’t apply any more rules.
    • QSA – Appends any query string from the original request URL to custom pages. It means that calling http://www.example.com/page1?foo=bar&c=1 will be rewrited internally into /custom/page1.php?foo=bar&c=1

    Please note:

    1. if you already have a page named “page1” in your wordpress site, this page will be overridden by custom page added into rewrite rule.
    2. These Apache httpd directives were standard and generated by WordPress, configuring Permalink Settings. I have just added the custom part.
    3. Any time you change Permalink Settings, your .htaccess will be overwritten, better if you change .htaccess permission making the file readonly. And, in any case, save a copy of your .htaccess configuration.

    On the other hand if you only want your WordPress having URLs more SEO friendly, i.e. URLs without .php pages and without the full query strings but only the post/page slug as unique identifier, you may access to WordPress Permalink Settings and select Post name or the option you prefer:

    Wordpress permalink settings

    When you activate Permalink Settings, Apache httpd work is limited to the .htaccess part. This means that Apache redirects everything (request header and body) via .htaccess to the index.php page and WordPress implements internally every rewrite.
    In case you want more suggestions related to WordPress configuration, I’ll suggest to write your questions in https://wordpress.stackexchange.com/

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

    Well, this will not leave .php on index, but at least will work without 500 internal server errors. Why do you need to keep index.php ? Login.php will have the old format.

  3. WordPress has URL rewriting built in without manually modifying the htaccess file. You can choose how you would like your URLs to display in the WordPress admin under “settings”->”permalinks”.

    WordPress also provides a class, WP_Rewrite, that you can use to further tweak the URL if you need to.

  4. The most obvious answer to your question is to add a RewriteCond before your RewriteRule

    RewriteCond %{REQUEST_URI} !^/(index|login).php$
    

    This would accomplish exactly what you want. Any request that comes in except for index.php and login.php will get .php added to it by your rule so requesting them without works.

    Currently your rule turns index.php and login.php into index.php.php and login.php.php. A more logical way of solving it would probably be to add this instead:

    RewriteCond %{REQUEST_URI} !.*php$
    

    Then it just turns anything without .php into the same with .php

    Please note that your RewriteRule is not stripping the query string even though you said you wanted it to. It will strip the query string if you change it into:

    RewriteRule ^(.*)$ $1.php?
    

    If you do not add the ? it will append the query_string by default