Modify friendly URLs generated by default in Wordpress (via .htaccess)
First you should really understand what those rules are doing and what you really want to achieve. Then you can try to change the system to fit your needs.
IfModule
ensures everything inside is processed only when mod_rewrite Apache module is present. All the other directives are from this module.
RewriteEngine On
enables URL rewriting.RewriteBase /
tells the engine that the rules operate inside root. See also the general question on howRewriteBase
works.RewriteRule ^index\.php$ - [L]
means that no more rules should be processed ([L]
) if the current URL to be rewritten isindex.php
. No rewrite takes place.RewriteRule
directive accepts a regex. See also regex tag here on SO.- All
RewriteCond
directives apply to the followingRewriteRule
. Unless[OR]
flag is added, they must be all satisfied at the same time to execute the rule. In this case they mean:- Requested resource is not a regular file.
- Requested resource is not a directory.
- Rewrite any (at least one character long) URL to
index.php
. This is the last ([L]
) rule to be processed.
When adding new RewriteRule
s, you probably want to use the WordPress way of doing this, as described in Zac’s answer. Figuring out the right rule by analogy to the many examples in the manual or here on SO should not be hard.
Put into functions.php maybe a better idea:
functions.php
function setRewriteRule($orgRules){ return array( '/([^/]+)/([0-9]+)/?' => 'index.php?post=$matches[1]' ) + $orgRules;}add_filter('rewrite_rules_array', 'setRewriteRule');
Then you just need flush the rewrite rules, I usually use 'rewrite-rules-inspector' plugin.
This should solve your problem give it a try ... you can write your own custom permalink without adding any code also if someone tries to access the page via old URL they will be redirected to the new one.
After installing this you just have to go into pages and type your own URL below the heading