I have the following URLs:
/products/?term_id=3
/products/?term_id=3&post_id=66
What I would like to get is the following:
/products/termid-termtitle
/products/termid-termtitle/postid-posttitle
Rewritten as the original URLs.
I've tried a few ones I have found elsewhere but to no avail.
Related
Basically I have a multiple language website. The URL of a selected language looks like this: website.com/?lang=EN
I have 30 languages. Is it possible to rewrite these URL's with applying a rewrite rule to cover them all to eg; website.com/en (website.com/"language")
check if the parameter exists and then use it for redirection, make sure to remove the parameter in your target url to avoid a redirect loop.
i.e. like this
if ($arg_lang!="") {
rewrite ^/$ https://website.com/$arg_lang?;
}
I'm trying to do a query string redirect to a more friendly url. This is within WordPress but I'm trying to do the mod_rewrite in the htaccess (am adding an extra rule, not touching any of the WordPress rules)
This is my desired url: localhost/site/events/term-1/term-2/term-3
And this is the actual url: localhost/site/events/?tax_1=term-1&tax_2=term-2&tax_3=term-3
Here is the code I have used:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^events/([^/]*)/([^/]*)/([^/]*)$ events/?tax_1=$1&tax_2=$2&tax_3=$3 [QSA,L]
</IfModule>
... WordPress rules here ...
This is giving me a 404 error when I go here: localhost/site/events/term-1/term-2/term-3 (but I have checked and localhost/site/events/?tax_1=term-1&tax_2=term-2&tax_3=term-3 is going to the correct place). I'm not getting any errors in the apache error log.
When I tested the rule on http://htaccess.mwl.be/ I was getting this result from the above input:
http://localhost/site/event/%3Ftax_1=term-1%26tax_2=term-2%26tax_3=term-3
which seems like it may be an encoding issue, but all the research I have done into the encoding of these inputs and outputs, this seems unlikely, and I'm wondering if this may be an issue with the tester.
Can anyone point out where I may be going wrong? I've been scouring the docs for mod_rewrites and I feel like my rules are written correctly. Is it an apache setting maybe? Or a conflict with WordPress?
Thanks
EDIT
Adding a NE flag to the rule is now giving me the correct result in the tester, but I am still getting the 404 result on the actual server.
I haven't technically solved my problem, but by using WordPress rewrite rules instead of mod_rewrite I have got the desired features.
For anyone else looking this what I added to my functions file:
function custom_rewrite_rule() {
add_rewrite_rule('^events/([^/]*)/([^/]*)/([^/]*)/?','index.php?page_id=PAGE_ID_OF_EVENTS_PAGE&tax_1=$matches[1]&tax_2=$matches[2]&tax_3=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_tag() {
add_rewrite_tag('%tax_1%', '([^&]+)');
add_rewrite_tag('%tax_2%', '([^&]+)');
add_rewrite_tag('%tax_3%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
Instead of hooking into the permalink events/, it needs to be sent to the index.php with the specified post id of where the page is, as the other custom rewrites will not run first, so WordPress won't know what events/ means.
The first function adds the rewrite rule and hooks it into the initialisation. The third parameter specifies when the rule will be run in the list of rules running - it has to be top as if it is bottom it will not run.
The second one adds the query vars as tags so that WordPress is aware of the custom query variables.
I tried different tutorials, answers on stack etc. - but can't seem to make this work on my particular case.
I'm trying to use Wordpress' Rewrite Rules to rewrite an ugly URL containing a variable, making it look nice and SEO-friendly. But whatever I do, it doesn't seem to apply. Tried printing $wp_rewrite->rules which returns my custom rule at the top, so I guess the system works as it should, however URL isn't being rewritten.
Here's the function in functions.php:
function create_new_url_querystring()
{
add_rewrite_tag('%hovedgruppe%', '([^&]+)');
add_rewrite_rule(
'(.+)/?$',
'index.php?page_id=75&hovedgruppe=$matches[1]',
'top'
);
}
add_action('init', 'create_new_url_querystring');
The URL I'm testing this on is www.domain.com/produkter/gruppe/?hovedgruppe=value (which means /produkter/gruppe is a page with ID 75). The ideal result would be www.domain.com/value
Tried different variations of the rewrite rule. And yes I'm flushing the rewrite rules when making changes (entering the permalink page in wp-admin). Anyone able to understand how the rewrite rule should be written to make it work?
Never mind! It actually works as it is, I just didn't realize it was working, because I was expecting the URLs to automatically change from non-rewritten to rewritten. It doesn't, but if I enter a rewritten URL, it works. :D
I'm trying to figure out how to redirect the permalink structure for some of my very old WordPress news portals, which are using funky structures that were needed for Google News and old search engines, but now are crap.
For example, I currently have:
mynewssite.xcom/2015/03/19/fnw14770_161419.php
Where I'd rather have:
mynewssite.xcom/pretty-nameo-of-the-article-fnw14770/
I would even be okay with this instead:
mynewssite.xcom/pretty-name-of-the-article-fnw14770_161419.php
My current WordPress permalink structure is:
/%year%/%monthnum%/%day%/fnw%post_id%_%hour%%minute%%second%.php
I need to do a 301 redirect to something like /%postname%/ or /%postname%-%post_id%/, or in a worst case senario, something like this:
%postname%-fnw%post_id%_%hour%%minute%%second%.php
I don't know how to write rewrite expressions. This is what I currently have now, but it's not working:
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/fnw(\d+)_%hour%%minute%%second%.php$ http://floridanewswire.com/?p=$4
%hour%%minute%%second% won't work in .htaccess, as this is meta information known only to WordPress. Instead, try replacing it with a rule that matches six digits, such as \d{6}
Also, you should remove unnecessary capture groups, escape forward sashes and dots, and use \d instead of [0-9].
Here is a fixed and simplified version of your expression:
RedirectMatch 301 ^\/\d{4}\/\d{2}\/\d{2}\/fnw(\d+)_\d{6}\.php http://floridanewswire.com/?p=$1
You should be able to change your permalinks to a cleaner structure on the Settings > Permalinks admin menu and have the old permalinks redirected through this htaccess rule.
I am new to the URL rewrite rules and would like to rewrite some URLs. Below is my problem:
I have created two rules (which works fine) that will hide the *.php extension from the URL. One is to redirect "/abc.php" to "/abc". Another rule is to Rewrite /abc as /abc.php. These two rules work fine and I use the following pattern.
^([^/]+)/?\.php$ --> Redirects to --> {R:1}
^([^/]+)/?$ --> Rewrites to --> {R:1}.php
Now I have a problem. I have a page which goes like www.example.com/member?getuser=chris (note that it's not member.php?getuser=chrisyeung because of the previous rules). I want to rewrite the URL to:
www.example.com/member/chris
I tried to follow some tutorials and use the following pattern:
`^/member/(_[0-0a-z-]+)` --> Rewrites to --> `^member?getuser={R:1}`.
...but it doesn't work.
What am I missing?
I don't unterstand your first two rules. Rewrite *.php to * and * to *.php - sounds like a loop for me. I assume your files are named .php and you want your visitors use URLs w/o this file extension. Then just rewrite * to *.php.
For your members pages, use:
^/member/([0-9a-zA-Z-_]+) member.php?getuser={R:1} [QSA]
I changes the list of valid characters inside the [] and I call member.php directly - no need for issuing another rule. QSA is for passing through further URL params.