I have a website done in WordPress and I need to make some changes in the fiendly URLs.
Iâve created a page from the admin panel named detail
, this page reads the template file detail.php
from the templates folder.
The URL that is currently mounted is http://www.domain.com/detail/1234/
and I need that it could be accessed as http://www.domain.com/anything/1234/
.
The following lines have been generated by WordPress but I donât understand them and I donât know how to modify them for my purpose:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
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.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: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
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.
WordPress Custom Permalinks
After installing this you just have to go into pages and type your own URL below the heading