• I want to change the url of my wordpress, I have read that I can make that with rewrite rule but it does not work me unfortunately. Here are the details:

    I have a wordpress theme, which got me that kind of URL:
    www.my-theme.pl/portfolio_category/red-flower/

    I want to rewrite the URL via .htaccess to receive:
    www.my-theme.pl/gardens/red-flower/

    As you see, I just want to replace /portfolio_category/ with /gardens/ text in the URL.

    I entered this rule:

    RewriteRule ^/portfolio_category/$ /gardens/$ [NC]

    And it does not work unfortunately. Can you clarify how to use properly ?
    Below is my entire .htaccess code:

    `Options -Indexes
    AuthType Basic
    AuthName “password”
    AuthUserFile /home/*******************/.htpasswd
    Require valid-user

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

    </IfModule>
    # END WordPress`

    The ******* are replaced with working path to my htpasswd.
    It is my first time to make any rewrite changes and I am not familiar with it. I think I have read the tutorials correctly but the change still does not work.

    Best regards from Poland,
    Jacek

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    Dzień dobry! (That’s nearly all the Polish I know ?? )

    You shouldn’t edit inside the WordPress block. If you or a plugin ever changes the WP Rewrite API rules or the permalink settings, WP will overwrite your rule. Create your own rewrite block, including IfModule, engine on, the whole thing. Place it above the WP block.

    You generally need a condition to match before applying a rule. The first WP index\.php rule is a rare exception which prevents the main rule from falling into an infinite loop. You also don’t want to use the $ symbol, that indicates to the engine to only match specified text that is at the end of the URL, that nothing occurs after the character before the $.

    I can’t provide a working rewrite block for you. One small error can break your site. I don’t want to be responsible for that, sorry.

    You’d actually be better off using the Rewrite API anyway. You still need to come up with the correct RegExp for it to work. That can be a challenge in itself. There’s a few online RegExp tools that let you experiment until you get it right. Sort of a jsfiddle for RegExp. here’s one: https://www.regexr.com/

    Thread Starter doktor-x

    (@doktor-x)

    Thank you very much for your answer. Could you help me and point me to the rewrite rule I should use ? The site is in preparations only, its private site of mine with images, so it wont go public until all error will be fixed. Right now it works fine but I need that rewrite syntax to change the portfolio_category text in URL.

    This portfolio thing is always the same and always after the main site address: https://www.my-theme.pl

    Im totaly green about rewrite rule in htaccess and rewrite api of wordpress. I do not know where start even and find that portfolio_category thing to replace :/

    And it makes the url of my site ugly unfortunately.

    Moderator bcworkz

    (@bcworkz)

    I initially simply tried to just answer your question. In reviewing this thread, I realized there is likely more to this than either of us realized. First of all, I think your rewrite attempts are reversed or you’re confused about what is getting rewritten.

    There’s two sides to this. One is that you want (I think) permalinks that appear on your site to read “gardens” instead of “portfolio_category”. This is not accomplished by rewrites, it must be done when the permalink is output, usually with a filter.

    When the link is clicked, the “gardens” must then be rewritten to something that WP understands, the “portfolio_category”. All of this is certainly possible, but ignores the “elephant in the room” (idiom for something obvious) “portfolio_category” is a custom taxonomy, right?. You really only need to change the rewrite slug where the taxonomy is declared. This will take care of both the permalink and the rewrite sides at once.

    How this is done would depend on where the declaration occurs. There could be a filter, or if this is custom code, simply edit directly. Or add a new taxonomy and use that instead.

    Thread Starter doktor-x

    (@doktor-x)

    Thanks for reply, you are right. It is custom taxonomy.
    Could you help me change it when I paste here the code where I found this custom taxonomy so I will not break something in the code ?

    Moderator bcworkz

    (@bcworkz)

    Yes, I can help with that. Pasting just the taxonomy declaration here is fine, but if you need to post a lot of code for any reason, please use pastebin.com, then provide the link here. When you save the paste there, select PHP syntax highlighting. It makes reading the code much easier.

    Thread Starter doktor-x

    (@doktor-x)

    Here are the links for my files which could contain this thing:

    functions.php – https://pastebin.com/uFz1mJT2
    taxonomy-portfolio_category.php – https://pastebin.com/FzvmE1G6
    custom_functions.php – https://pastebin.com/RwajB3Bd

    I hope it helps…

    Moderator bcworkz

    (@bcworkz)

    Thanks for doing that, but unfortunately I didn’t find the taxonomy declaration in the first two links. The paste for the last link was removed, I suspect it was proprietary code.

    Generally speaking, we can’t help people who use proprietary code because we cannot access the source code. That doesn’t mean you can’t alter your own code with a little guidance. The hardest part here is just finding the declaration. The Unix/Linux command grep is really useful for this, it is capable of recursively doing full text searches in a directory structure. You would need terminal access to your server to run it though, most people on low cost shared hosting do not AFAIK.

    You could download via FTP your plugins and active theme folders to your local computer and search them that way. If you’re using Windows, I know there’s a grep equivalent utility you can download that’ll do the same thing.

    Search for “register_taxonomy” where the first argument afterwards is “portfolio_category”. Here is a sample register_taxonomy() call for the ‘genre’ taxonomy, applied to the ‘post’ post type:

    register_taxonomy(
          'genre',
          'post',
          array(
             'labels' => array(
             	'name' => 'Genres',
             	'singular_name' => 'Genre',
             	'edit_item' => 'Edit Genre',
             	'add_new_item' => 'Add New Genre',
             ),
             'rewrite' => array(
             	'slug' => 'genre',
             	'with_front' => true,
             	'hierarchical' => true,
             ),
             'hierarchical' => true,
             'sort'=> true,
          )
       );

    In your case the rewrite arguments should be something like this:

    'rewrite' => array(
             	'slug' => 'portfolio_category',

    Changing the slug ‘portfolio_category’ to ‘gardens’ or whatever shouldn’t impact any related code but will alter how related permalinks appear. In fact, you should be able to alter any of the arguments in the arrays to the right of ‘=>’ as long as they are strings and not true or false. You’ll need to switch your permalink setting back and forth to get the new slug to be recognized by the rewrite system.

    You should understand that altering code this way is an issue because your changes will be overwritten when the theme or plugin declaring it is updated. And ignoring updates so you do not lose custom changes is a very bad idea. There could be a way to do this alteration such that the changes persist through updates, but there’s too many variables involved for me to speculate on how that might be done.

    Powodzenia!

    Thread Starter doktor-x

    (@doktor-x)

    Dear bcworkz, thank you for your reply.
    I tried to search in all php files of my theme – in 159 files, “portfolio_category” existed in 21 separate files. I tried help in other places, the problem was there was not any “register” or “register_taxonomy” in any of the files.

    But I hired a coder, he created a plugin for my Wp and he used the rewrite slug option to the one I needed.
    The theme code was too much customised by the authors to work with as far as I know and it would not work as a child theme. Plugin solved all my problems and possible future updates.

    I really thank you for trying to help me and I wish you all well.

    Best regards!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! I’m glad you found a solution, even if I had nothing to do with it. There’s nothing like a paid expert with full access to your site to get something working ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Rewrite rule for custom category in a theme, how to change URL addres’ is closed to new replies.