Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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
like image 906
fish man Avatar asked Jan 15 '23 10:01

fish man


1 Answers

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/

like image 179
freedev Avatar answered Jan 28 '23 14:01

freedev