Create a new htaccess file for wordpress
Here is an example of a basic .htaccess
file for a WordPress website:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Add a trailing slash to URLs that are missing one
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^(.*)$ $1/ [R=301,L]
# Rewrite rules for WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule. /index.php [L]
</IfModule>
# END WordPress
Let me explain what each line does:
# BEGIN WordPress
: This line marks the beginning of the WordPress-specific configuration.<IfModule mod_rewrite.c>
: This line checks if the Apache modulemod_rewrite
is available. If it is, the code inside this block will be executed.RewriteEngine On
: This line enables the rewrite engine, which allows us to rewrite URLs.RewriteBase /
: This line sets the base URL for the rewrite rules. In this case, it's set to the root of the website (/
).RewriteCond %{REQUEST_FILENAME}!-f
: This line checks if the requested URL is not a file. If it's not a file, the rule will be applied.RewriteRule ^(.*)$ $1/ [R=301,L]
: This line adds a trailing slash to URLs that are missing one. TheR=301
flag specifies that this is a permanent redirect, and theL
flag specifies that this is the last rule to be applied.RewriteRule ^index\.php$ - [L]
: This line specifies that theindex.php
file should not be rewritten.RewriteCond %{REQUEST_FILENAME}!-f
: This line checks if the requested URL is not a file. If it's not a file, the rule will be applied.RewriteRule. /index.php [L]
: This line rewrites all URLs that are not files to theindex.php
file. TheL
flag specifies that this is the last rule to be applied.</IfModule>
: This line marks the end of the WordPress-specific configuration.# END WordPress
: This line marks the end of the WordPress-specific configuration.
You can add more rules to this file as needed, but be careful not to overwrite the existing rules. It's also a good idea to backup your existing .htaccess
file before making changes.
Note: If you're using a WordPress plugin that requires specific rewrite rules, you may need to add those rules to this file as well.