• Hi,
    I’m using a rewriterule for my website:

    RewriteCond %{THE_REQUEST} pagename=immagini&gallerytag=([^\s\?]+)\s [NC]
    RewriteRule ^ immagini/ngg_tag/tags/%1? [R=301,L]
    # Internaly rewrite
    RewriteRule ^immagini/tags/(.*)/?$ /pagename=immagini&gallerytag=$1 [QSA,NC,L]

    Works fine, but when I’ll put the mouse cursor on the link, I see a not rewrited url, ex:
    https://www.site.com/?pagename=immagini&gallerytag=shoes
    Exist a way to mask a non friendly url also in html code? I not want see the posted parameters in url.

    Thanks
    Angelo

Viewing 15 replies - 1 through 15 (of 17 total)
  • I’m guessing you added this to the .htaccess file?

    If so, take a look at adding these rules using the WordPress functions add_rewrite_tag and add_rewrite_rule. By adding it to WordPress, it can output the pretty permalink.

    Thread Starter anjx

    (@anjx)

    Yes, I’ve added rewriterule in .htaccess.
    Now I tried to insert add_rewrite_rule in function.php but the situation don’t change:

    add_rewrite_rule(
    ‘^immagini/ngg_tag/tags/([^/]*)/?’,
    ‘pagename=immagini&gallerytag=$matches[1]’,
    ‘top’
    );

    As regards add_rewrite_tag, I do not need of a permalink tag. It is necessary?

    After you’ve added the rewrite rule, you need to flush the old rewrite rules from the WordPress database so that it can read your new ones. You can do that by visiting the Settings > Permalinks page in the WordPress admin.

    With regard to the rewrite tags, I always add that when I’m using custom query vars, just to make WordPress aware that they exist, however, I don’t think you need them. The nice thing is that those values will also then be available in $wp_query on the page.

    Thread Starter anjx

    (@anjx)

    Nothing. Not work ??

    Are the rules you put in the .htaccess file still there? If so, go ahead and remove them.

    Are you getting an error page or is it not redirecting? What are you seeing?

    Thread Starter anjx

    (@anjx)

    Yes, are in htaccess. If I’ll removed it, the redirect not work and the url is:
    https://www.site.com/?pagename=immagini&gallerytag=shoes

    Before you add all of your code, try a simpler example, so you can troubleshoot any issues along the way.

    For example, remove everything you added from the .htaccess file, add a simple rewrite rule:

    add_rewrite_rule(
    	'^immagini/?',
    	'index.php?page_id=2',
    	'top'
    );

    Flush the permalinks and then visit your URL, i.e., example.org/immagini which should load your page that has ID 2. Replace the page_id value with the ID of your immagini page (or any other page).

    If this loads, then continue adding your other pieces.

    Thread Starter anjx

    (@anjx)

    Thank you Ryan for your help.
    So, I tried:

    add_rewrite_rule(
    	'^immagini/?',
    	'index.php?page_id=13281',
    	'top'
    );

    And I receive correct link:
    https://www.site.com/immagini

    So, I tries this code:

    add_rewrite_rule(
    	'^immagini/?',
    	'index.php?pagename=immagini',
    	'top'
    );

    And receive a wrong url, but correct page (immagini):
    https://www.site.com/?pagename=immagini

    Then, I tried this:

    add_rewrite_rule(
    	'^immagini/ngg_tag/tags/([^/]*)/?',
    	'index.php?page_id=13281&gallerytag=$matches[1]',
    	'top'
    );

    But I receive this url:
    https://www.site.com/immagini?gallerytag=shoes
    With correct page but not friendly url.

    Where is the bug?

    EDIT:
    After added this code:

    add_rewrite_rule(
    	'^immagini/?',
    	'index.php?page_id=13281',
    	'top'
    );

    I’ve seen that the same trouble: the link are not friendly but they have just the correct redirect. Then I see this link:
    https://www.site.com/index.php?page_id=13281

    In both cases above, what do you mean by “receive” that URL? Where are you getting that URL from? Are you entering that in the address bar of the browser or are you calling a function that is returning the incorrect URL? (If so, what is the function?)

    If you visit example.org/immagini, it should stay on that URL and the page should load, but are you saying that it’s redirecting you to the URL with all the query vars in the URL?

    Thread Starter anjx

    (@anjx)

    I talk about of the link into the address bar of the browser:

    If I insert this:
    https://www.site.com/index.php?page_id=13281

    I’m redirect to:
    https://www.site.com/immagini

    Moderator bcworkz

    (@bcworkz)

    That will happen if the slug for page_id 13281 is immagini. In that case there is no purpose for the added rewrite. The rewrite is for loading a particular page using an URL not otherwise recognized by WP.

    Aren’t we chasing after the solution to the wrong problem here? I thought the purpose here was to hide query vars or URL parameters so that only the main permalink is seen, correct?

    If so, rewrites are not the solution. You need a different way to pass the information contained in the query vars besides as URL parameters, such as user meta, transients, sessions, or cookies.

    Additionally, if you want the browser to show the new URL instead of the entered or clicked URL, I believe your .htaccess rewrite rule would need to include the full URL including http, your domain, etc. I don’t think there is anyway for WP rewrite rules to accomplish this, they always show the entered or clicked URL, never the new one.

    If I’ve misinterpreted, my apologies. I still think there is some confusion in the intended user experience here, we should ensure we all understand exactly what this is before getting into the messy details of how to accomplish this.

    Thread Starter anjx

    (@anjx)

    Hi bcworkz,
    can you suggest me a code to insert in htaccess, please?

    @anjx

    If I insert this:
    https://www.site.com/index.php?page_id=13281

    I’m redirect to:
    https://www.site.com/immagini

    Yes, I thought you’re trying to mask the query var and have it translate to a friendly URL?

    If so, your next step would be to include the gallerytag, read that on the immagini page (in the immagini template), and then retrieve the data matching the gallerytag, which you can get the value from $wp_query (which is a global).

    Exist a way to mask a non friendly url also in html code?

    An earlier question I asked was how you’re generating the URL in the HTML? Are you calling a function, and if so, what is the name of it?

    @bcworkz

    If so, rewrites are not the solution. You need a different way to pass the information contained in the query vars besides as URL parameters, such as user meta, transients, sessions, or cookies.

    Based on the first post, the final solution also includes a gallery tag, which can only be supplied in the URL, so for that, I think he does need a rewrite if he wants to mask the URL.

    @anjx

    If I’m not addressing your issue at all, maybe try and explain your setup, e.g., whether you are working with custom post types, taxonomies, custom page templates, what are the query var components, how are they utilized, etc.

    Once we have a better understanding of what you’ve built, maybe we can understand your goal and help you find a way to solve it.

    Thread Starter anjx

    (@anjx)

    Ryan you’re always very kind.
    I simply include a list of link:

    <ul>
    <li><a href="https://www.site.com/index.php?pagename=immagini&gallerytag=schoes">Shoes</a></li>
    <li><a href="https://www.site.com/index.php?pagename=immagini&gallerytag=clothes">Clothes</a></li>
    <li><a href="https://www.site.com/index.php?pagename=immagini&gallerytag=beauty">Beauty</a></li>
    </ul>

    Based at tag name (ex shoes) I’ll show a different gallery.
    No function.

    i would like that the links were automatically rewritten in html:

    <ul>
    <li><a href="https://www.site.com/immagini/schoes">Shoes</a></li>
    <li><a href="https://www.site.com//immagini/clothes">Clothes</a></li>
    <li><a href="https://www.site.com/immagini/beauty">Beauty</a></li>
    </ul>

    OK, thank you for explaining that.

    Is immagini a WordPress page? Can you clarify where the tags are coming from? Are you using a WordPress taxonomy?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Rewriterule and mask original link from html’ is closed to new replies.