I am looking for a solution to do bulk 301 redirects in WordPress. I have looked at some common htaccess redirects but cannot find the right solution for what I need.
Current URL format:
website.com/year/month/post-name (e.g website.com/2020/02/post-name)
Format I would like to achieve:
website.com/blog/post-name
The post slug will always remain the same, it's only the first part of the URL that needs adjusting.
You could match the format of the request URI like year/month/post-name with regex in RewriteCondition and have a rewrite rule for it if the request URI matches.
Snippet:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/?\d+\/\d+\/.+ [NC]
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [NC,L,QSA]
Demo: https://htaccess.madewithlove.be?share=f2c421fe-e23d-50a9-8988-9a7bac647951
In the above rule, we perform a case insensitive match and grab the post-name part in group 1 of regex which is (.+). Now, we add that in our redirection URL with a $1 where the 1 is the group number(as group number 0 is the entire regex itself).
Update:
You can add a R redirection signal with status code of 302 if this is a temporary redirect, or a permanent redirect 301 (which will updated in SEO crawl engines as well).
To do so, you can change the rewrite rule from
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [NC,L,QSA]
to
RewriteRule ^\d+\/\d+\/(.+) http://website.com/blog/$1 [R=302,NC,L,QSA]
Related
On the home page of my website, I have a guide that includes some links, and each redirects to a family guide page, all with the redirection by identification of each element of the guide, remaining as the link
https://example.com/family-guide/?id=4
Where '4' is the ID of the element.
I would like to leave like this:
https://example.com/family-guide/4
I tried to use mod_rewrite in the .htaccess file:
RewriteRule ^family-guide/([0-9] +)/([0-9a-zA-Z_-]+) family-guide/?Id=$1name=$2 [NC, L]
but nothing happens
Your rule has some spaces. Be careful because if there is a space between the flags [NC, L] the rule is not valid.
Another thing is that you want the url https://example.com/family-guide/4 to match a rule and your rule expects two parameters (id and name) while in this url there is only one.
I would use a couple of rules instead:
RewriteRule family-guide/([0-9]+)/([0-9a-zA-Z_-]+) family-guide/?id=$1&name=$2 [NC,L]
RewriteRule family-guide/([0-9]+) family-guide/?id=$1 [NC,L]
With this rules if you go to https://example.com/family-guide/4 you should be shown the content of https://example.com/family-guide/?id=4. And if you go to https://example.com/family-guide/4/whatever it will show you the content of https://example.com/family-guide/?id=4&name=whatever instead, which is what I understood you need.
Also, the rule order is important as if they were in the opposite order https://example.com/family-guide/4/whatever would match the other rule and show the content of https://example.com/family-guide/?id=4/whatever
I have no knowledge in regex and never done regex redirections.
htaccess
but... I am looking for a way to redirect this URL structure:
http://www.example.com/mypage/param1/
to this URL structure
http://www.example.com/mypage/?key1=param1
and, if possible, only if param1 contains # or %40
I saw this answer:
How to change URL structure with htaccess?
but that's the reveresed direction for my question
RewriteEngine on
RewriteCond $1 #
RewriteRule ^mypage/((?!index).+)$ /mypage/?key=$1 [NC,L,R]
Remove the R flag if you dont want the url to change.
This will redirect an url of the form :
/mypage/.*#.*
to
/mypage/?key=.*#.*
We have several URLS and want to re-direct based on one of the segments.
Usually we've have the rewrite rule then something like:
redirect permanent /home/category/feline /home/category/cat
The challenge is there's something like 50+ urls and the segment changes, so I wanted to know if there is a simpler way to match and redirect?
Here's a few examples:
redirect permanent /home/category/feline /home/category/cat
redirect permanent /content/category/feline /home/category/cat
redirect permanent /channel/segment/category/feline /channel/segment/category/cat
redirect permanent /segment/category/feline/entry /segment/category/cat/entry
Is is possible to redirect any URLs with feline to cat?
I'm currently testing the following, which works, but wanted to clarify:
RewriteRule (.*)feline(.*)$ $1cat$2 [R=301,NE,L]
You can use RedirectMatch with regex support:
RedirectMatch 301 ^(.*/)?feline(/.*)?$ $1cat$2
Clear your browser cache before testing this rule.
Using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*/)?feline(/.*)?$ /$1cat$2 [R=301,NE,NC,L]
I am trying to figure out how to setup some redirects in my htaccess file.
We load our stores with dynamic urls for example:
/locations/new-york
new-york being the dynamic part
We have old urls that contain different path such as
/locations/new-york/local-store
I want to be able to take all locations/dynamic-city and forward them to the new URL
So
/locations/new-york/local-store/offers
should goto /locations/new-york/current-offers
RewriteRule ^locations/(.*)/local-store/offers locations/$1/current-offers [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/hot-tubs locations/$1/hot-tubs [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/pre-owned locations/$1/pre-owned [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/client-reviews locations/$1/reviews [L,R=301,QSA]
Ok, so I'm not seeing a way to easily use 1 rewriterule to cover all 4 cases. But you can see make sure the requested url following a pattern like this.
^locations/{location}/local-store/offers
Then you take the location and place it into the new url you want to write it to.
L # means it's the last rule, don't try to match anymore
R=301 # means redirect the old url to the new one
QSA # means append any query string parameters to the new url
Hey everyone I search around for this answer but could only find ways to redirect each URL individually.
My old site urls were like this:
www.domain.com/show_detail.php?item=item_description_of_various_lengths
new URLs are like this:
www.domain.com/shop/brands/titan/item_description_of_various_lengths/
Now I know how to redirect these on a 1 by 1 basis. I used the following code:
RewriteCond %{QUERY_STRING} ^item=item_description_of_various_lengths$ [NC]
RewriteRule show_detail.php http://www.domain.com/product-category/paint-spraying/hvlp-fine-finish/item_description_of_various_lengths/? [R=301,L]
Which works great and all but after writing about 200 of these I'm just wondering if there is a way to redirect ALL pages starting with show_detail.php to my home page and call it a day. Or even better 1 rule that will just redirect all of them to a search page which searches the "item_description_of_various_Lengths" phrase automatically.
You can use a regex catpure group to get value of the Item dynmically
RewriteCond %{QUERY_STRING} ^item=([^&]+)$ [NC]
And then replace description_of_v_length with %1? in the target url.