URL Rewriting in Wordpress htaccess - wordpress

I've built a Wordpress site that has a product page which retrieves product info from an external system using the querystring id passed in. So www.domain.com/product/?id=n loads the Wordpress product page which then loads the product info using the id.
This works fine.
However, I want to rewrite this URL so that:
www.domain.com/product/productname/id retrieves www.domain.com/product/?id=n
When I add the following to htaccess, the page returns a 404 because it doesn't exist in Wordpress.
RewriteRule ^product/productname/(.*)$ ^product/?id=$1 [L]
Does anyone know how to write this rule in such a way that it doesn't get hijacked by Wordpress rewriting?

After doing some additional searching, I've found some links that may help you get started. The secret lies in WordPress's add_rewrite_ruleshook. Haven't tried these myself yet, but looks promising:
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
URL rewriting via Wordpress Rewrite or .htaccess
http://www.hongkiat.com/blog/wordpress-url-rewrite/
http://matty.co.za/2009/11/custom-url-rewrites-in-wordpress/

Related

How to modify URL Structure as per our requirement

As per the SEO standards, I want to modify my URL Structure.
For example :
I have a domain name
domain.com/blog/
on which I have installed WordPress.
Now while posting any blog I am getting my URL as
domain.com/blog/category-name/post-name
Now, I need to have my post URL as
domain.com/category-name/post-name
Similarly, for each category and its related post, I need to modify the URL structure as mentioned above.
Keeping in mind that my domain.com is developed on any other platform, and we have to modify WordPress installed directory/URL domain.com/blog as per our requirement.
Subfolder name coming in between domain name and category name while editing it with WordPress admin dashboard custom permalink.
Not having any idea about this URL structure modification.
Your help will be highly appreciated.
Thank You.
With ,htaccess
This should change domain.com/blog/ to domain.com/
RewriteEngine On
RewriteRule ^/blog/(.*)$ domain.com/$1 [L,R=301]
Install Wordpress directly to domain.com, instead of sub folder 'blog'. Then URL will work as expected.
Otherwise can simply use this plugin:
https://wordpress.org/plugins/permalink-manager/
The plugin works with all custom post types and taxonomies, as well as many popular third-party plugins.

how to rewrite non exist url to display content of dynamic page from wordpress

in wordpress normally if click domain.com/index.php?p=2
it redirect to domain.com/sample-page/
say now i like to show sample-page content domain.com/sample-page/
from non exist url
domain.com/member/abc/
domain.com/member/bbb/
domain.com/member/ccc/
but when user run any of above URL example.com/member/xxx/ in browser it will show content of example.com/sample-page/
without any redirection to example.com/sample-page/
so the address still remain in example.com/member/xxx/
please advice how to do in .htaccess or in functions.php
This sounds pretty straight forward, all you need is a single internal rewrite rule:
RewriteEngine on
RewriteRule ^/?member/.+/?$ /sample-page/ [END]
Such rule has to be implemented to be applied prior to the wordpress internal rules.

How can I use htaccess to redirect paths with a wildcard character in wordpress

I recently changed my 92 category links and now I am getting hit with hundreds of 404 errors from Google because the old links are not found. I tried to use plugins to automatically redirect single URLS but I am noticing there are more and more coming in and doing everything on a 1 by 1 basis is not efficient.
I was hoping someone could assist me on getting my pages redirected using my htaccess file.
For Example Going to
example.com/category/old-category/
should redirect to
example.com/category/new-category/
More importantly I would like to redirect the page numbers of the old categories to the page numbers of the new categories using a wildcard. So page x of old category will redirect to page x of new category.
example.com/category/old-category/page/5/
should redirect to
example.com/category/new-category/page/5/
Thanks in advance.
You can use this generic redirect rule just below RewriteEngine line:
RewriteRule ^(category)/old-category(/.*)? /$1/new-category/$2 [L,NC,R=301]

Wordpress. Rewrite url when on custom taxonomy

It's a wordpress site. I am using a plugin called media tags. This plugin create a custom taxonomy called 'media tags' to display media that has been tagged. The url for these pages are currently domain/blog/media-tags/results
I would like to remove the blog part only when on a media-tags page. So I end up with:
domain.com/media-tags/results
but the standard blog posts,categories & tags still keep the blog part of the url:
domain.com/blog/cat/post-title, domain.com/blog/cat, domain.com/tag/post-tag
Is this possible please?
I'm assuming you just need a snippet for your .htaccess file.
RewriteEngine on
# condition to avoid recursion:
RewriteCond %{REQUEST_FILENAME} !blog
# your rewrite rule:
RewriteRule media-tags blog/media-tags
When the server receives a request to domain.com/media-tags/results it will rewrite the target URL as domain.com/blog/media-tags/results.
I guess you will also have to configure your plugin to point the URLs to the root directory of your server. From the Media Tags plugin homepage:
You can also change this ‘/media-tags/’ URL parameter to something of your liking like ‘/gallery/’. To do this log into wp-admin and go to the Permalinks page under Settings. At the bottom of the page you will see the input field for Media-Tag. Simply enter your preference and update the page.

Changing category slug messes incoming links to posts in Wordpress

I have wordpress site on say: http://xyz.com/category-name/
now I changed the category slug to new-category-name
When I paste this in the browser:
http://xyz.com/category-name/post-name/
it redirects me to
http://xyz.com/new-category-name/
instead of
http://xyz.com/new-category-name/post-name/
I already tried
something like:
RewriteRule ^category-name/(.*) http://xyz.com/new-category-name/$1 [R=301,L]
but it does not work...
how can this be fixed?
It looks like the server cached the old .htaccess for a while. Now it started to work as expected.
Have you heard of the redirection plugin?
http://wordpress.org/extend/plugins/redirection/
One of the most powerful features is it tracks changes in post URL's and creates 301 redirects to the new pages automatically... saved me a ton of work.
Try refreshing the permalink structure going to Settings->Permalinks.

Resources