Remove index.php from Wordpress URLs

2019-08-21 00:33发布

I am new to Wordpress but tried all documentation and Stack Overflow posts for possible solutions but none of them have worked so far.

I have verified that mod rewrite is enabled and working as expected. Followed all steps mentioned here. https://wordpress.stackexchange.com/questions/105795/remove-index-php-from-permalinks

Also restarted Apache couple of times but still getting 404 errors when I remove index.php path from Permalink Settings.

Renamed the wordpress directory to blog to access the site at www.xyz.com/blog. Now the requirement is to access any blog posts with www.xyz.com/blog/2018/02/09/my-wp-post without index.php in the URLs.

We don't have any other CMS content other than WP for the blog site.

Permalink settings(Custom Structure): /index.php/%year%/%monthnum%/%day%/%postname%/

.htaccess file contents:

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

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-21 00:38

I spent ton of time trying many different approaches answered here or elsewhere but none of them fixed my issue.

This is what I did and it has fixed my issue.

  1. Change permalink settings to remove index.php
  2. Update/Save .htaccess content that's at the root of the WP installation if it's not allowed to be updated automatically when we change permalink settings in WP admin panel.
  3. This is the important step since not many Q & A mentioned this in detail, other than wordpress documentation here. Specifically check AllowOverride settings and change it to All, Apache httpd.conf will not load the .htaccess contents without this change. Of course mod_rewrite must be enabled in the server if it's not already done. In our case it's enabled by default, so didn't have to mess with this step.
  4. Also make sure FollowSymLinks option enabled as mentioned in the WP documentation.
  5. Last but not least, make sure you restart the Apache service/server for the changes to take effect.

Entry in httpd.conf file:

<Directory "/var/www/html/blog">
Options FollowSymLinks
AllowOverride All

查看更多
Explosion°爆炸
3楼-- · 2019-08-21 01:01

Assuming Ubuntu 16.04 & Apache2

1) Activate mod_rewrite. It's available but not enabled with a clean Apache 2 installation.

sudo a2enmod rewrite

2) Restart Apache

sudo systemctl restart apache2.service

3) Setup .htaccess

(Note: Apache reccomends using a server configuration file over inserting rules into .htaccess, however, for this example, inserting rules into .htaccess is sufficient because of the negligible performance hit.)

sudo nano /etc/apache2/sites-available/000-default.conf

Insert the following in 000-default.conf

<VirtualHost *:80>
    <Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

4) Restart Apache

Repeat step 2

5) Create .htaccess in the web root.

touch /var/www/html/.htaccess

Insert the following into .htaccess

RewriteEngine On

6 Configure the URL Rewrite (Note: Assuming index.php)

sudo nano /var/www/html/.htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

mod_rewrite is a useful Apache module that can be used effectively to ensure human-readable URLs and good SEO results. If you'd like to learn more about mod_rewrite, take a look at Apache's mod_rewrite Introduction and Apache's official documentation for mod_rewrite.

查看更多
登录 后发表回答