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
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
.If your site is available on
http://www.example.com
, your custom pages will be accessible usinghttp://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 callinghttp://www.example.com/page1?foo=bar&c=1
will be rewrited internally into/custom/page1.php?foo=bar&c=1
Please note:
Permalink Settings
. I have just added the custom part.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 toWordPress Permalink Settings
and selectPost name
or the option you prefer: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/
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.
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.
The most obvious answer to your question is to add a RewriteCond before your RewriteRule
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:
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:
If you do not add the ? it will append the query_string by default