Rewrite the page address in Wordpress

UPDATE

I tried using internal Wordpress chat. I need to make an address like this:

http://example.com/galleria/artist-name 

sent to gallery.php with a variable containing artist-name .

I used these rules according to the Wordpress documentation:

 // REWRITE RULES (per gallery) {{{ add_filter('rewrite_rules_array','wp_insertMyRewriteRules'); add_filter('query_vars','wp_insertMyRewriteQueryVars'); add_filter('init','flushRules'); // Remember to flush_rules() when adding rules function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); } // Adding a new rule function wp_insertMyRewriteRules($rules) { $newrules = array(); $newrules['(galleria)/(.*)$'] = 'index.php?pagename=gallery&galleryname=$matches[2]'; return $newrules + $rules; } // Adding the id var so that WP recognizes it function wp_insertMyRewriteQueryVars($vars) { array_push($vars, 'galleryname'); return $vars; } 

which is strange now, that in my local installation of Wordpress test, this works fine: the gallery page is called and the galleryname variable is galleryname . On the other hand, on the real site, the starting URL is accepted (as it does not go in 404), but it changes to http://example.com/gallery (I mean that it actually changes in the address bar of the browser) , and the variable is not defined in gallery.php .

Any idea what might cause this other behavior?

Alternatively, any other way that I could not think of that could achieve the same effect described in the first three lines is perfectly fine.


Old question

What I need to do is rewrite this address:

 (1) http://localhost/wordpress/fake/text-value 

to

 (2) http://localhost/wordpress/gallery?somevar=text-value 

Notes:

  • reassignment should be transparent: the user always needs to see the address (1)
  • gallery - permalink to wordpress page, not real address

I basically need to first rewrite the address (change it) and then return it back to mod rewrite again (so that wordpress analyzes it in its own way).

Problems

if i just do

 RewriteRule ^fake$ http://localhost/wordpress/gallery [L] 

it works, but the address in the browser changes, which is not good if I do

 RewriteRule ^fake$ /wordpress/gallery [L] 

I get 404. I tried different flags instead of [L] , but to no avail. How can I make this work?

EDIT: full .htaccess

 # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^fake$ /wordpress/gallery [R] RewriteBase /wordpress/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wordpress/index.php [L] </IfModule> # END WordPress 
+4
source share
4 answers

It was decided that this is a Permalink Redirect plugin that must be configured to skip the desired "virtual" URLs, otherwise it will issue 301 Constantly moving.

+1
source

Try the following rule:

 RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [L] 
+1
source

I apologize for the very delayed answer here, but I believe that you need to do what Gumbo suggested, but with the addition of the passthrough / PT flag :

This flag forces the rewrite mechanism to set the uri field of the internal request_rec structure to the value of the file name field.
...
You should use this flag if you want to mix directives from different modules that allow URL-to-filename translators.

Although Wordpress (via mod_php) starts after mod_rewrite completes, it usually gets the original (pre-rewritable) uri, and it uses this for its own internal rewriting, not for the rewritten value of the file name.

So try:

 RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [QSA,PT] 

I added QSA so that just in case someone goes to / wordpress / fake / something? page = 3, it will be passed correctly. Let me know how it works.

0
source
  • Using your favorite HTML / PHP editor, open wp-admin/includes/misc.php

  • Find the function function got_mod_rewrite Its around line 18 or so.

  • Comment on an existing function: /* this is commented out */ - You can remove this function if you are sure that the solution works, but to be safe, just comment on it if you need to return it.

  • Add the following instead:

     function got_mod_rewrite() { $got_rewrite = true; return apply_filters('got_rewrite', $got_rewrite); } 
  • Save and load.

-1
source

Source: https://habr.com/ru/post/1304021/


All Articles