htaccess rules before wordpress own rules - wordpress

I'm trying to add some rewrite rules to a Wordpress site, to go a little further than wp's own rewrite capabilities, but i am having trouble when I place those rules BEFORE wordpress own rules: if my rule rewrites to something that wordpress will rewrite later, it doesn't work. E.g:
www.domain.com/hello -> www.domain.com/image.jpg : this works, as wordpress doesn't interfere
www.domain.com/hello -> www.domain.com/category/1 : doesn't work, even if www.domain.com/category/1 works by itself
I've tried to remove the L tag from my rewrite rules, to allow further rewriting, but it doesn't seem to work...maybe wordpress does starnge things with htaccess?
Does this sound familiar to anyone?
Thank you

Wordpress doesn't check the rewritten url (/category/1), but parses the original request url (/hello), and so it doesn't know what to do with /hello. To fix this use the proxy flag. So [L] would become [L,P]

Related

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.

Remove Old Permalinks?

I have a Wordpress website. I have changed my permalinks several times. All past versions of the permalinks I used are still working. If I type them into the URL bar, I am taken to the page WITHOUT the URL changing/redirecting to the new/current permalink.
Why aren't the old permalinks redirecting to the new one?
This is causing issues with Google reading duplicate content.
Example: .com/lightbulb .com/shop/lightbulb
The second URL is old, but still works when I type it in.
Shouldn't it redirect to the new one?
How can I remove old permalinks from being found by Google?.
Thanks in advance
I think the old permalinks is stored in the table postmeta with the meta_key of _wp_old_slug
If you wanted to clear all of the old slugs you could use this:
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';
As you can see, Wordpress doesn't handle redirections when you change your permalinks structure. There is 2 ways to handle this, through:
The htaccess rewriting rules (manually editing the .htaccess file) depending on your new old/new permalinks structure. If .com/lightbulb is the new path and .com/shop/lightbul the old one:
Options -Indexes
Options -Multiviews
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RedirectMatch 301 /shop/lightbulb(.*) /lightbulb$1
# or
# RewriteRule ^shop/lightbulb?(.*) http://www.domain.com/lightbulb$1 [R=301,L]
</IfModule>
But if you want to redirect old permalink structure .com/shop/ to main domain your .htaccess rules will be:
Options -Indexes
Options -Multiviews
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RedirectMatch 301 /shop/(.*) /$1
# or
# RewriteRule ^shop/?(.*) http://www.domain.com/$1
</IfModule>
Permanent redirection is used for best SEO practices, redirecting definitively old permalinks to new ones, avoiding duplicate content, and telling search engines that your old permalinks have been moved definitively to new ones.
Redirection Wordpress free plugins (there is a lot):
Redirections plugin is the most popular and offer a easy-to-manage 301 redirections and 404 error tracking. It also offers more advanced tools to help you keep track of loose ends on your site like broken links and orphan pages.
Simple 301 redirects plugin is simple and deals with 301 redirect creation for when you’ve permanently moved content from one location to another.
Quick Page/Post Redirect plugin easily redirect pages/posts or custom post types to another page/post or external URL by specifying the redirect URL and type (301, 302, 307, meta).
Safe Redirect Manager plugin comes with an additional whitelist feature for added security…
SEO Redirection Plugin plugin offers support for generating 301, 302, and 307 redirects and also supports 404 error monitoring with easy one-click redirection…
Converting old path (urls) in database (if they still exist):
You can use Search and Replace free plugin, to easily find some old related urls or path in your database and bulk replace them by the new ones. This is a very powerful plugin, a little buggy, but working fine when you know it (but make always a backup before).
A recent reference:
A Simple Guide to Changing Your Permalinks Without Breaking Your WordPress Website
Why aren't the old permalinks redirecting to the new one?
Each time you add new permalink structure and flush rewrite rules, WordPress doesn't redirect old links to new links. Instead, WordPress will alter the old links with the new links completely.
All past versions of the permalinks I used are still working.
In WordPress, a URL doesn't map to route. It's looped through rewrite rules to find a match - a real URL which can be translated into database queries by parse_request method.
It means that if the old rewrite rules which match old links aren't removed. The old links still work.
How can I remove old permalinks from being found by Google?
Since they have been indexed by Google, I think permanent redirects is the best solution.
I assume you're using Apache, if not, please let me know.
In your case, just add following code before WordPress rules in your .htaccess file:
RewriteEngine On
RewriteRule "^shop/(.+)/?$" "http://example.com/$1" [R=301,L]

Custom mod rewrite (rewriting urls for WP multisite network) in .htaccess

I'm having this strange problem.
WP Multi Site Network has been created and pages that existed before were moved into it.
Everything works fine except, in old pages that was coded entirely in html, subsites were of format:
http://domain-a.com/subsite1.html
http://domain-b.com/subsite2.html
but now with WP pretty links they look this way:
http://domain-a.com/subsite1
http://domain-b.com/subsite2
Seo agency would like to make automagic rewrite of old links to new one. I have found sample htpassword redirrect that looks this way:
RedirectMatch 301 (.*).html$ http://domain.com$1/
but it will not work correctly because there is domain name on right but none on the left.
I understand, that for each domain I should create different rule, because proppably it cant be done in one rule globally, but either case I dont know how to do it correctly.
In theory rewrite should take any address that is with .html at the end and rewrite it to exact same without .html at the end.
Instead of your rule try this rule:
RedirectMatch 301 ^/(.*).html$ /$1/

How to internally add /blog/ to the url with .htaccess?

Hi I have wordpress installation in subfolder /blog/, as it has to co-exist with other legacy cms, so now if i wish to access some page of wordpress i need to use links as
www.domain.com/blog/news/ and would like to rather use www.domain.com/news/.
So basically for certain links i need to add this /blog/ somehow internally so wordpress
would handle it normally.
Please note it will be few links so rewrite rules can be literal.
Rewriting URI's for wordpress can always be kind of sketchy because wordpress has its own way to do routing and may not play nicely with other rewriting sometimes, but you can try:
RewriteRule ^/?news/some-page$ /blog/news/some-page [L]
As long as they're before any other wordpress related rules.

Mapping a subdomain to a Wordpress page

Any recommendations or best practices for mapping a subdomain to a Wordpress page:
http://my-page.mydomain.com -> http://mydomain.com/my-page
I don't want to do a redirect either, just keep the original subdomain URL until a different link is click, and then the subdomain is removed.
I'm using Apache 2 with mod_rewrite, wordpress 2.7 with pretty-urls, and php 5.
As already mentioned I would use mod_rewrite to get this done. Something like the following should work in the my-page.mydomain.com virtual host
RewriteEngine on
RewriteRule (.*) http://mydomain.com/my-page$1 [P,L]
This will require you to enable mod_proxy. Please read the security implications of doing this on the Apache website http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
URL Rewriting. I would start here
What you want can be achieved with mod_rewrite.
Keep in mind that google will detect the two pages as duplicates, which may hurt your pagerank.

Resources